The Code for KLIPA
//get the values from the input
//CONTROLLER (start) function
function getValues(){
//get values from the page
let startValue = document.getElementById("startValue").value;
let endValue = document.getElementById("endValue").value;
let numbers = [];
//parse into integers
startValue = parseInt(startValue);
endValue = parseInt(endValue);
//validate numbers
if (Number.isInteger(startValue) && Number.isInteger(endValue)) {
//call generateNumbers
numbers = generateNumbers(startValue, endValue);
//call displayNumbers
displayNumbers(numbers)
} else {
alert("Enter only integers");
}
}
//generate numbers form the start value to the end value
//LOGIC function(s)
function generateNumbers(start, end){
let numbers = [];
//get all numbers from start to end
for(let i=start; i<=end; i++){
numbers.push(i);
}
return numbers;
}
//display numbers and mark even numbers bold
//VIEW function(s)
function displayNumbers(numbers){
let templateRows = "";
for(let i=0; i ${number} `;
}
document.getElementById("results").innerHTML = templateRows;
}
The Code is structured as follows.
getValues
getValues is a function that uses DOM manipulation methods, such as getElementById to retrieve user input for start value and end value. Then numbers array is declared to store values between start value and end value.
parseInt
parseInt validates the input type, if any value entered is not of integer data type it is automatically converted to an integer.
If Else
The if statement validates that both start value and end value are numbers.
Then generateNumbers and displayNumbers functions are called.
Otherwise, the user is alerted to only enter integers as input.
generateNumbers
generateNumbers function receives start value and end value as parameters. An array is declared. Start and end values are used in a for loop to add all numbers between first and last value to numbers array. Then numbers array is returned.
displayNumbers
NB: displayNumbers does NOT render corectly with prism.
But displayNumbers function displays all number from start to end according to use input, furthermore all even numbers are displayed in bold.