Last active
August 1, 2017 07:36
-
-
Save kogai/661a419eab24311394024020bfb98171 to your computer and use it in GitHub Desktop.
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 assert = require('assert'); | |
| const deepEqual = assert.deepEqual; | |
| const isFizz = x => x % 3 === 0; | |
| const isBuzz = x => x % 5 === 0; | |
| const isFizzBuzz = x => isFizz(x) && isBuzz(x); | |
| const match = x => { | |
| if (isFizzBuzz(x)) return 'FizzBuzz'; | |
| if (isFizz(x)) return 'Fizz'; | |
| if (isBuzz(x)) return 'Buzz'; | |
| return x; | |
| }; | |
| assert(match(1) === 1); | |
| assert(match(3) === 'Fizz'); | |
| assert(match(5) === 'Buzz'); | |
| assert(match(15) === 'FizzBuzz'); | |
| const fizzBuzz = (x, max, buf = []) => { | |
| if (x > max) return buf; | |
| return fizzBuzz(x + 1, max, buf.concat([match(x)])); | |
| }; | |
| deepEqual(fizzBuzz(1, 20), [1, 2, 'Fizz', 4, 'Buzz', 'Fizz', 7, 8, 'Fizz', 'Buzz', 11, 'Fizz', 13, 14, 'FizzBuzz', 16, 17, 'Fizz', 19, 'Buzz']); | |
| fizzBuzz(1, 100).forEach(x => console.log(x)); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment