Skip to content

Instantly share code, notes, and snippets.

@tparveen
Created June 15, 2018 12:33
Show Gist options
  • Save tparveen/17a5640ebcb9671420fd18f229fff6a7 to your computer and use it in GitHub Desktop.
Save tparveen/17a5640ebcb9671420fd18f229fff6a7 to your computer and use it in GitHub Desktop.
Fizzbuzz with modularization
const fizzbuzz = function(num){
if(num%15 === 0){
return 'fizzbuzz';
}
if(num%5 === 0){
return 'buzz';
}
if(num%3===0){
return 'fizz';
}
return num;
};
//console.log(fizzbuzz(151));
//lets show this in the browser so create the HTML
const generateFizzBuzzHtml = function(n){
let fizzClass = '';
if(typeof n === 'string'){
fizzClass = n;
}
//console.log(fizzClass);
//console.log(`<div>class='fizzbuzzItem ${fizzClass}' ${n} </div>`);
return `<div class="fizz-buzz-item ${fizzClass}"><span>${n}</span></div>`;
};
const handleSubmit = function(){
$('#number-chooser').on('submit',event => {
event.preventDefault();
//get value from users
const userInput = $('#number-choice').val();
//what to do with the input box afterwards? either make it blank or reset it
//$('#number-choice').val('');
//$(event.target).reset();
//I have an input from the user and I want to evaluate it to fizzBuzz
const fizzBuzzItems = [];
//take user input and start from 1 to that input
for(let i=1; i<=userInput; i++){
fizzBuzzItems.push(fizzbuzz(i));
}
const html = fizzBuzzItems.map(item=>generateFizzBuzzHtml(item));
$('.js-results').append(html);
//console.log(fizzBuzzItems);
});
};
function main(){
handleSubmit();
}
$(main);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment