The Code for BleepBoop
// Validation on user input
function isValid() {
// Get Values from Page
let bleepValue = parseInt(document.getElementById('bleepValue').value);
let boopValue = parseInt(document.getElementById('boopValue').value);
// Call Empty Table
emptyTable();
// Validates input (makes sure numbers are within rage, bleep:2-9 | boop 3-10)
if (bleepValue < 2 || bleepValue > 9 || boopValue < 3 || boopValue > 10 || bleepValue === "" || boopValue === "") {
alert('Please enter a correct Bleep Value (2-9) or Boop Value (3-10)');
} else if (bleepValue === boopValue) { // or if they are the same
alert('Please enter different Bleep and Boop Values: Bleep and Boop cannot be the same.');
} else {
iterateValues(bleepValue, boopValue);
}
}
// Empty the Table of values on the page
function emptyTable() {
let table = document.getElementById('results');
while (table.firstChild) {
table.removeChild(table.firstChild);
}
}
// Sequentially goes through numbers 1 through 100
function iterateValues(bleepValue, boopValue) {
let start = 1;
let end = 100;
for (let num = start; num <= end; num++) {
if (bleepBoop(num, bleepValue, boopValue)) { // if divisible by both
printValues('BleepBoop');
} else if (bleep(num, bleepValue)) { // if divisible by one
printValues('Bleep');
} else if (boop(num, boopValue)) { // if divisible by the other
printValues('Boop');
} else {
printValues(num);
}
}
}
// Output Values to Page
function printValues(printValue) {
let tr = document.createElement('tr');
let td = document.createElement('td');
document.getElementById('results').appendChild(tr).appendChild(td).innerHTML = printValue;
}
// This function checks if the current iteration number is divisible by the bleep and boop values
function bleepBoop(num, bleepValue, boopValue) {
if (num % bleepValue === 0 && num % boopValue === 0) {
return true;
} else {
return false;
}
}
// This function checks if the current iteration number is divisible by the bleep value
function bleep(num, bleepValue) {
if (num % bleepValue === 0) {
return true;
} else {
return false;
}
}
// This function checks if the current iteration number is divisible by the boop value
function boop(num, boopValue) {
if (num % boopValue === 0) {
return true;
} else {
return false;
}
}
The Code is structured in the following functions:
isValid
This function checks to see if the user inputs are valid and not just empty values or values out of range. Also tucked in here is a clearing of any previous tables generated by the user by calling the emptyTable function
emptyTable
This function loops through the table children and deletes each one until the table is empty. Can be called upon by the user from the page as well.
iterateValues
This function takes in the user inputs and loops through numbers 1-100 and calls to see if the current iteration is divisible by the user inputs. It then calls the printValues function and sends to desired output.
printValues
This function creates table row, table data, and appends them to the current table as well as the passed value.
bleepBoop
This function returns true if the passed value is divisible by the passed user input, false if it isn't.
bleep
This function returns true if the passed value is divisible by the passed user input, false if it isn't.
boop
This function returns true if the passed value is divisible by the passed user input, false if it isn't.