Skip to content

Instantly share code, notes, and snippets.

@gouf
Created Jun 7, 2022
Embed
What would you like to do?
interface Number {
isFizzBuzz(): boolean
isFizz(): boolean
isBuzz(): boolean
}
Number.prototype.isFizzBuzz = function(): boolean {
return this.isFizz() && this.isBuzz()
}
Number.prototype.isFizz = function(): boolean {
return Number(this) % 3 === 0
}
Number.prototype.isBuzz = function(): boolean {
return Number(this) % 5 === 0
}
function fizzBuzz(n: number): string {
if (n.isFizzBuzz()) {
return 'FizzBuzz'
} else if (n.isFizz()) {
return 'Fizz'
} else if (n.isBuzz()) {
return 'Buzz'
} else {
return n.toString()
}
}
function printFizzBuzz(n: number): void {
console.log(fizzBuzz(n))
}
// main
for(let i = 1; i <= 100; i++) {
printFizzBuzz(i)
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment