Nam Ngo

From www.norsemathology.org

Jump to: navigation, search


Contents

1. Summary

The web program was written by html, css and javascript. The program calculates the estimation of number of infected cases based on the number of people, who have already been infected, the growth rate, which is the rate that the people get infected per day (For example: the number of people getting infected is doubled per day or increase by 1.5 per day) and number of days have passed. The main formula for calculation was based on recursion.


2. Code

Here is the javascript code containing recursion method:

// Nam Ngo // Program take user inputs of number of infected cases, growth rate per day and day passed to calculate estimation of number of infected cases after certain days

// Selecting Elements

const btnCal = document.querySelector("#btnCal");

const btnReset = document.querySelector("#btnReset");

const numInfectedInput = document.querySelector("#numInfected");

const growthRateInput = document.querySelector("#growthRate");

const dayPassedInput = document.querySelector("#dayPassed");

const result = document.querySelector("#result");

// Inputs

let numInfected = null;

let growthRate = null;

let dayPassed = null;

// Calculate Button

btnCal.addEventListener("click", function () {

 result.textContent = `The Estimated Number of Infected Cases is ${calculate(
   numInfected,
   growthRate,
   dayPassed
 )}`;

});

// Number of Infected Case Input

numInfectedInput.addEventListener("keydown", function (e) {

 if (e.key === "Enter") {
   numInfected = numInfectedInput.value;
   numInfectedInput.disabled = true;
 }

});

// Growth Rate Input

growthRateInput.addEventListener("keydown", function (e) {

 if (e.key === "Enter") {
   growthRate = growthRateInput.value;
   growthRateInput.disabled = true;
 }

});

// Day Passed Input

dayPassedInput.addEventListener("keydown", function (e) {

 if (e.key === "Enter") {
   dayPassed = dayPassedInput.value;
   dayPassedInput.disabled = true;
 }

});

// Calculate Result Recursion

function calculate(numInfected, growthRate, dayPassed) {

 if (dayPassed <= 0) {
   return numInfected;
 } else {
   return growthRate * calculate(numInfected, growthRate, dayPassed - 1);
 }

}

// Reset Button

btnReset.addEventListener("click", reset);

// Reset Function

function reset() {

 numInfectedInput.disabled = false;
 growthRateInput.disabled = false;
 dayPassedInput.disabled = false;
 numInfectedInput.value = null;
 growthRateInput.value = null;
 dayPassedInput.value = null;
 result.textContent = null;

}


3. Overview Image

For example, here is the result when the infected case number is 10, the rate of infection is 3 and the number of days has passed is 3.

When you hit calculate button the result will be shown at the bottom. In this example, the estimation number of infected cases is 270.

You could hit reset button to clear your input and result.

Image:overview.png

4. All Source Code PDF

Here is the PDF of source code of all the HTML, CSS and JavaScript files:

Media:SourceCode.pdf

Personal tools