The Code for Hundo:
function getValues() {
var x = parseInt(document.getElementById("startValue").value);
var y = parseInt(document.getElementById("endValue").value);
if(x <= 0 || y <=0 || x > 100 || y >100)
{
document.getElementById("startValueId").classList.add("text-info", "fw-bold");
document.getElementById("endValueId").classList.add("text-info", "fw-bold");
document.getElementById(
"startValueId").innerText =
"Please enter a Numeric value greater than 0 and less than 100";
document.getElementById(
"endValueId").innerText =
"Please enter a Numeric value greater than 0 and less than 100";
}
else{
MakeArray(x,y);
}
}
function makeArray(int1,int2) {
var arr = new Array;
var count = 0;
var num = 0;
for (i = 1; i < 101; i++) {
if(i % int1 == 0 && i % int2 ==0)
{
num ="FizzBuzz"';
}
else if(i % int1 == 0 && i % int2 !=0)
{
num ="Fizz";
}
else if(i % int1 != 0 && i % int2 ==0)
{
num ="Buzz";
}
else {num = i;}
if(count == 0)
{
el = `${num}`;
}
else if(count % 5 == 0)
{
el =`${num}`;
}
else{ el =`${num}`}
arr[count] = el;
count++;
}
displayResults(arr)
}
function displayResults(arr)
{
var arr2 = new Array;
// join method is for specifically adding all elements of an array together into one big string.
arr2 = arr.join("")
document.getElementById("results").innerHTML = arr2;
}
The Code for displaying the Hundo results is structured into 3 parts (or functions):
GetValues()
The first piece of Javascript code used is a function called GetValues(). GetValues()
is called by the Onclick event handler within the button element in the HTML markup for the
application page when the User clicks the "Display the Hondo" button. GetValues() first checks
(line 5) if the user entered any values below 1 or over 100. If so, it uses the getElementById()
function to append the appropriate element with the information to ask the user to enter acceptable values.
Next (line 17), providing the user has entered acceptable values, GetValues() calls another function - makeArray()) to deal with
the next step in the process, which is creating the values of Hondo to display to the user.