Skip to content

Instantly share code, notes, and snippets.

@Davidblkx
Created September 30, 2019 20:50
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 Davidblkx/2e6470420fae88c3cb8b1ae7d79aa612 to your computer and use it in GitHub Desktop.
Save Davidblkx/2e6470420fae88c3cb8b1ae7d79aa612 to your computer and use it in GitHub Desktop.
Simple FizzBuzz in typescript
console.log('A basic FizzBuzz in typescript')
const START_NUMBER = 1;
const LAST_NUMBER = 100;
const FIZZ = 'Fizz';
const BUZZ = 'Buzz';
function calcFizzBuzz(n: number): string | number {
let toPrint = '';
if ((n % 3) === 0) {
toPrint = FIZZ
}
if ((n % 5) === 0) {
toPrint += BUZZ
}
return toPrint || n;
}
const result: (string | number)[] = [];
for (let i = START_NUMBER; i <= LAST_NUMBER; i++) {
result.push(calcFizzBuzz(i))
}
result.forEach(n => console.warn(n));
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment