Skip to content

Instantly share code, notes, and snippets.

@FaustXVI
Last active February 22, 2019 15:57
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save FaustXVI/4c5a40a8fe55697e828e89f837ed8bdc to your computer and use it in GitHub Desktop.
Save FaustXVI/4c5a40a8fe55697e828e89f837ed8bdc to your computer and use it in GitHub Desktop.
"use strict";
import {should} from "chai";
should();
function fizzbuzz(n) {
const rules = [{multiple: 3, word: "fizz"}, {multiple: 5, word: "buzz"}];
return rules.filter(({multiple, _}) => n % multiple === 0)
.map(({_, word}) => word)
.reduce((a, b) => a + b, "")
.replace(/^$/, `${n}`);
}
/*
function fizzbuzz(n) {
const results = [`fizzbuzz`, `buzz`, `fizz`, `${n}`];
let resultIndex = Math.min(n % 3, 1) | (Math.min(n % 5, 1) << 1);
return results[resultIndex];
}
function fizzbuzz(n) {
return (n % 3 + "" + n % 5)
.replace(/^0/, "fizz")
.replace(/0$/, "buzz")
.replace(/[1-9]/g, "")
.replace(/^$/, `${n}`);
}
*/
describe('Fizzbuzz', () => {
for (const n of [3, 6]) {
it(`fizz for multiples of 3 (${n})`, () => {
fizzbuzz(n).should.equal("fizz");
});
}
for (const n of [5, 10]) {
it(`buzz for multiples of 5 (${n})`, () => {
fizzbuzz(n).should.equal("buzz");
});
}
for (const n of [15, 30]) {
it(`fizzbuzz for multiples of 5 and 3 (${n})`, () => {
fizzbuzz(n).should.equal("fizzbuzz");
});
}
for (const n of [1, 2]) {
it(`echo ${n}`, () => {
fizzbuzz(n).should.equal(`${n}`);
});
}
});
@YDanot
Copy link

YDanot commented Feb 22, 2019

private boolean fizzbuzz(int i) { return (fizz(i) | buzz(i)) || number(i); }

private boolean fizz(int i) { return isFizz(i) && printFizz(); }

private boolean buzz(int i) { return isBuzz(i) && printBuzz(); }

private boolean number(int i) { System.out.print(i); return true; }

private boolean isFizz(int i) { return i % 3 == 0; }

private boolean isBuzz(int i) { return i % 5 == 0; }

private boolean printFizz() { System.out.print("FIZZ"); return true; }

private boolean printBuzz() { System.out.print("BUZZ"); return true; }

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