Created
June 15, 2018 12:33
-
-
Save tparveen/17a5640ebcb9671420fd18f229fff6a7 to your computer and use it in GitHub Desktop.
Fizzbuzz with modularization
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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