Created
June 7, 2022 08:30
-
-
Save gouf/34c22a28509527b70f840fbf54afa532 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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