Skip to content

Instantly share code, notes, and snippets.

@AlenVelocity
Created July 13, 2021 14:12
Show Gist options
  • Save AlenVelocity/b6207c0678397135863023aeedcba88c to your computer and use it in GitHub Desktop.
Save AlenVelocity/b6207c0678397135863023aeedcba88c to your computer and use it in GitHub Desktop.
type Filter<T> = {
[K in keyof T as Exclude<K, 'run' | 'set'>]: T[K]
}
class FizzBuzz {
constructor(public max: number = 100, public fizz: number = 3, public buzz: number = 5) {
}
set = (key: keyof Filter<FizzBuzz>, num: number) => {
this[key] = num
return this
}
run = () => {
let out = ''
for (let i = 1; i <= this.max; i++) {
let temp = ''
temp += Number.isInteger(i/this.fizz) ? 'fizz' : ''
temp += Number.isInteger(i/this.buzz) ? 'buzz' : ''
out += temp? `${temp}\n` : `${i}\n`
}
return out
}
}
console.log(new FizzBuzz(100, 3, 5).run())
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment