Last active
January 11, 2018 10:58
-
-
Save selfish/61b81bcda8156ada858ab3a9cab8c70d to your computer and use it in GitHub Desktop.
FizzBuzz
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
/** | |
* FizzBuzz | |
* Instruction: Write a program that prints the numbers from 1 to 100. | |
* But for multiples of three print "Fizz" instead of the number and for the | |
* multiples of five print "Buzz". For numbers which are multiples of both three | |
* and five print "FizzBuzz". | |
**/ | |
// Common dynamic implementation, vanilla: | |
function fizzbuzz(start = 0, end = 100, dict = {3: 'Fizz', 5: 'Buzz'}) { | |
for (let i = start; i <= end; i++) { | |
let word = ''; | |
for (const divider in dict) { | |
if (i % divider === 0) { | |
word += dict[divider]; | |
} | |
} | |
console.log(word || i); | |
} | |
} | |
// Minimal static implementation, vanilla: | |
fb = () => for(i=0;i++<100;console.log((i%3?'':'Fizz')+(i%5?'':'Buzz')||i)); | |
// Minimal dynamic implementation, vanilla: | |
fb = (start = 0, end = 100, dict = {3: 'Fizz', 5: 'Buzz'}) => for(i=start;i++<end;console.log(Object.keys(dict).map(k=>i%k?'':dict[k]).join('')||i)); | |
// Minimal Dynamic implementation with Lodash, single print: | |
fb = (start = 0, end = 100, dict = {3: 'Fizz', 5: 'Buzz'}) => console.log(_(_.range(start, end + 1)).map(i => _(dict).keys().map(k => i % k === 0 ? dict[k] : null).compact().join('') || i).join('\n')); | |
// https://blog.codinghorror.com/why-cant-programmers-program/ |
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
/** | |
* Reverse 3 Multiples | |
* Instruction: Write a program that prints out, in reverse order, | |
* every multiple of 3 between 1 and 200. | |
* | |
* Reationale: Doing it in normal order it easy: multiply the loop index by 3 | |
* until you reach a number that exceeds 200, then quit. You don't have to worry | |
* about how many iterations to terminate after, you just keep going until you | |
* reach the first value that's too high. But going backwards, you have to know | |
* where to start. Some might realize intuitively that 198 (3 * 66) is the | |
* highest multiple of 3, and as such, hard-code 66 into the loop. Others might | |
* use a mathematical operation (integer division or a floor() on a floating | |
* point division of 200 and 3) to figure out that number, and in doing so, | |
* provide something more generically applicable. | |
**/ |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment