Last active
July 31, 2017 15:47
-
-
Save davismj/fdffc6f0d56bddf4a5563a789469a635 to your computer and use it in GitHub Desktop.
FizzBuzz (https://www.youtube.com/watch?v=QPZ0pIK_wsc)
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
| function fizzBuzz(first, second, iterations = 100, output = (value) => console.log(value)) { | |
| first = parseInt(first); | |
| second = parseInt(second); | |
| const third = first * second; | |
| iterations = parseInt(iterations); | |
| if (isNaN(third) || isNaN(iterations) || typeof output !== 'function') { | |
| return console.error('Proper usage: fizzBuzz(first: Number, second: Number, iterations: Number, output: Function)'); | |
| } | |
| for (let i = 1; i <= iterations; i++) { | |
| if (i % third === 0) { | |
| output('fizzbuzz'); | |
| } else if (i % second === 0) { | |
| output('buzz'); | |
| } else if (i % first === 0) { | |
| output('fizz'); | |
| } else { | |
| output(i); | |
| } | |
| } | |
| } | |
| const outputEl = document.body; | |
| fizzBuzz(3, 5, 100, (val) => { | |
| outputEl.innerText += val + '\n'; | |
| console.log(val); | |
| }); | |
| fizzBuzz('a', 3); | |
| fizzBuzz(5, 'b') | |
| fizzBuzz(1, 2, 1, 2); |
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
| <!doctype html> | |
| <html lang="en"> | |
| <head> | |
| <meta charset="utf-8"> | |
| <title>FizzBuzz</title> | |
| </head> | |
| <body> | |
| <pre id="output"></pre> | |
| <script src="fb.js"></script> | |
| </body> | |
| </html> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment