Skip to content

Instantly share code, notes, and snippets.

@gouf
Created June 7, 2022 08:30
Show Gist options
  • Save gouf/34c22a28509527b70f840fbf54afa532 to your computer and use it in GitHub Desktop.
Save gouf/34c22a28509527b70f840fbf54afa532 to your computer and use it in GitHub Desktop.
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