Fizz Buzz: The Code
function fizzBuzz(val1, val2) {
let returnValue = "";
/*generate numbers from 1 to 100, evaluate against values taken in
* if divisible by val1, print 'FIZZ' - by val2, print 'BIZZ'
* if it's divisible by both, print 'FIZZBUZZ'*/
for (let i = 1; i <= 100; i++) {
returnValue += ((i % val1 == 0 ? 'FIZZ' : '') + (i % val2 == 0 ? 'BUZZ' : '') || i) + ' ';
}
return returnValue;
};
//display results
$("#buzzIt").on('click', function() {
let output = '';
let val1 = $("#numIn1").val();
let val2 = $("#numIn2").val();
output = fizzBuzz(val1, val2);
$('#results').empty().append(output);
});
//clear all
$("#clearIt").on('click', function () {
$("#numIn1").val("");
$("#numIn2").val("");
$("#results").text("");
})
//force numbers only input
$(".numOnly").keydown(function (event) {
let valid = false;
// Allow backspace, tab, and delete
if (event.keyCode == 46 || event.keyCode == 8 || event.keyCode == 9) {
// let it happen
valid = true;
} else {
// Ensure that it is a number and stop the keypress
if (event.keyCode >= 96 && event.keyCode <= 105) {
valid = true;
}}
});