The Code for Fully Charged.
// Get values from user inputs
// Start/controller function
function getValues() {
// Get values from page inputs
let startValue = document.getElementById("startValue").value;
let endValue = document.getElementById("endValue").value;
// Parse input into integers
startValue = parseInt(startValue);
endValue = parseInt(endValue);
// Validate inputs then continue
if (Number.isInteger(startValue) && Number.isInteger(endValue)) {
// Call generateNumbers
let numbers = generateNumbers(startValue, endValue);
// Call displayNumbers
displayNumbers(numbers);
} else {
alert("You must enter integers");
}
}
// Generate numbers fom startValue to endValue
// Logic function(s)
function generateNumbers(sValue, eValue) {
let numbers = [];
// Get numbers from start to finish
for (let i = sValue; i <= eValue; i++) {
numbers.push(i);
}
return numbers;
}
// Display the numbers and mark evens as bold
// Display/view functions
function displayNumbers(numbers) {
// Variable to hold HTML
let templateRows = "";
for (let index = 0; index < numbers.length; index++) {
let className = "even";
let number = numbers[index];
// Checks if number is even
if (number % 2 === 0) {
className = "even";
} else {
className = "odd"
}
// Template literals do not render correctly.
// Refer to the repo for correct code below.
templateRows += `${number} `;
}
document.getElementById("results").innerHTML = templateRows;
}
The Code is structured in three functions.
getValues
Once the 'Go!' button is hit, the start/end value inputs are saved and validated as integer values. If validation passes, we continue with these values as they're passed to our next function(generateNumbers). If validation fails, an alert will pop up and the code will stop.
generateNumbers
generateNumbers begins to iterate through each integer(starting startValue & ending at endValue) in increments of 1. Each value is pushed into the numbers array. This array is then returned at the end, completing this function. There is a final function call for displayNumbers that applies these numbers to the HTML results.
displayNumbers
displayNumbers takes the numbers array returned from the previous function and inputs each into a template literal string(each number generating a table cell string). Once iteration is complete, we apply it to the innerHTML of our results table.