Created
September 2, 2017 04:14
-
-
Save jonataa/414f90d764d062230d3c755b02f33fbb 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'); | |
| function getFizzBuzzRules() { | |
| return [ | |
| { label: () => 'fizzbuzz', rule: n => n % 15 === 0 }, | |
| { label: () => 'buzz', rule: n => n % 5 === 0 }, | |
| { label: () => 'fizz', rule: n => n % 3 === 0 }, | |
| { label: (n) => n, rule: n => n === n }, | |
| ]; | |
| } | |
| function sequenceToFizzbuzz(sequence, separator = ', ') { | |
| return sequence.map(number => { | |
| const rule = getFizzBuzzRules().find(item => item.rule(number)); | |
| return rule.label(number); | |
| }).join(separator); | |
| } | |
| describe('Fizzbuzz', function() { | |
| it('translate sequence 1, 2, 3, 4 and 5 to fizzbuzz', function() { | |
| assert.equal('1, 2, fizz, 4, buzz', sequenceToFizzbuzz([1, 2, 3, 4, 5])); | |
| }); | |
| it('translate sequence 3, 5 and 15 to fizzbuzz', function() { | |
| assert.equal('fizz, buzz, fizzbuzz', sequenceToFizzbuzz([3, 5, 15])); | |
| }); | |
| it('translate sequence 1, 2 and 3 to fizzbuzz', function() { | |
| assert.equal('1, 2, fizz', sequenceToFizzbuzz([1, 2, 3])); | |
| }); | |
| }) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment