Skip to content

Instantly share code, notes, and snippets.

@eriktrom
Created October 21, 2014 19:16
Show Gist options
  • Save eriktrom/115ce1ee2b3e6c3b4709 to your computer and use it in GitHub Desktop.
Save eriktrom/115ce1ee2b3e6c3b4709 to your computer and use it in GitHub Desktop.
FizzBuzz
describe('FizzBuzz', function () {
it('prints "Fizz" for multiples of 3, "Buzz" for 5 and "FizzBuzz" for multiples of 3 or 5', function () {
var expected = [
1,
2,
"Fizz",
4,
"Buzz",
"Fizz",
7,
8,
"Fizz",
"Buzz",
11,
"Fizz",
13,
14,
"FizzBuzz",
16,
17,
"Fizz",
19,
"Buzz",
"Fizz",
22,
23,
"Fizz",
"Buzz",
26,
"Fizz",
28,
29,
"FizzBuzz",
31,
32,
"Fizz",
34,
"Buzz",
"Fizz",
37,
38,
"Fizz",
"Buzz",
41,
"Fizz",
43,
44,
"FizzBuzz",
46,
47,
"Fizz",
49,
"Buzz",
"Fizz",
52,
53,
"Fizz",
"Buzz",
56,
"Fizz",
58,
59,
"FizzBuzz",
61,
62,
"Fizz",
64,
"Buzz",
"Fizz",
67,
68,
"Fizz",
"Buzz",
71,
"Fizz",
73,
74,
"FizzBuzz",
76,
77,
"Fizz",
79,
"Buzz",
"Fizz",
82,
83,
"Fizz",
"Buzz",
86,
"Fizz",
88,
89,
"FizzBuzz",
91,
92,
"Fizz",
94,
"Buzz",
"Fizz",
97,
98,
"Fizz",
"Buzz"
].join("\n");
var actual = _.chain(_.range(1, 101))
.reduce(function (memo, num) {
if (num % 3 === 0 && num % 5 === 0) memo.push("FizzBuzz");
else if (num % 3 === 0) memo.push("Fizz");
else if (num % 5 === 0) memo.push("Buzz");
else memo.push(num);
return memo;
}, []).join("\n")
.value();
expect(actual).to.eq(expected);
});
});
@kiwiupover
Copy link

 var actual = _.chain(_.range(1, 101))
      .reduce(function (memo, num) {
       output = '';

        if (num % 3 === 0) output += "Fizz";
        if (num % 5 === 0) output +="Buzz";

        memo.push( output || num);

        return memo;
      }, []).join("\n")
      .value();

@eriktrom
Copy link
Author

@kiwiupover - lol, just saw this

One thing though, given they return the same result, I would argue mine is easier to reason about down the road or when doing code review.

Thoughts? Agree or disagree?

@eriktrom
Copy link
Author

Also, @kiwiupover - let me know your thoughts on my for loop criticism/critique if your curious. https://gist.github.com/trombom/cc5c56be2abf890ac880

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment