Skip to content

Instantly share code, notes, and snippets.

@ulisesantana
Created May 15, 2020 22:04
Show Gist options
  • Save ulisesantana/850b7c2e730c5a704b09fe1720db465b to your computer and use it in GitHub Desktop.
Save ulisesantana/850b7c2e730c5a704b09fe1720db465b to your computer and use it in GitHub Desktop.
Fizz Buzz TDD with Deno
import fizzBuzz, { FIZZ, BUZZ, FIZZ_BUZZ } from "./fizzBuzz.ts";
import { assertEquals } from "https://deno.land/std/testing/asserts.ts";
Deno.test(
"FizzBuzz should return the same number given if is not divisible by 3 or 5",
() => {
assertEquals(fizzBuzz(1), 1);
},
);
Deno.test(
'FizzBuzz should return "Fizz" if the number given is divisible by 3',
() => {
assertEquals(fizzBuzz(6), FIZZ);
},
);
Deno.test(
'FizzBuzz should return "Buzz" if the number given is divisible by 5',
() => {
assertEquals(fizzBuzz(25), BUZZ);
},
);
Deno.test(
'FizzBuzz should return "FizzBuzz" if the number given is divisible by 5 and 3',
() => {
assertEquals(fizzBuzz(15), FIZZ_BUZZ);
},
);
import {
bgWhite,
magenta,
cyan,
bold,
} from "https://deno.land/std/fmt/colors.ts";
const isDivisibleBy = (x: number) => (y: number) => y % x === 0;
const isDivisibleBy3 = isDivisibleBy(3);
const isDivisibleBy5 = isDivisibleBy(5);
const useBold = (color: Function, text: string): string => bold(color(text));
export const FIZZ = useBold(magenta, "Fizz");
export const BUZZ = useBold(cyan, "Buzz");
export const FIZZ_BUZZ = useBold(bgWhite, FIZZ + BUZZ);
export default function fizzBuzz(num: number): string | number {
if (isDivisibleBy3(num) && isDivisibleBy5(num)) {
return FIZZ_BUZZ;
}
if (isDivisibleBy3(num)) {
return FIZZ;
}
if (isDivisibleBy5(num)) {
return BUZZ;
}
return num;
}
import fizzBuzz from "./fizzBuzz.ts";
Array.from({ length: 100 }).forEach((irrelevant, i) => {
console.log(fizzBuzz(++i));
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment