Hey future me (or whoever stumbles on this), Remember that classic FizzBuzz puzzle? The one that trips up folks in interviews because it's deceptively simple but tests your basics: loops, conditionals, modulo ops.I write code in TypeScript today—why? 'Cause TS is my go-to for front-end stuff lately, and it's fun to see how the typing shakes things up. Plus, practicing cross-lang implementations keeps the brain sharp.
Quick recap: Print numbers from 1 to 100 (or whatever range), but swap in "Fizz" for multiples of 3, "Buzz" for 5, and "FizzBuzz" for both. Easy peasy, but let's make it robust with params, error checks, and TS flair.
The Code Snippet
Here's the function. I tucked it in a module-style export for reusability. Drop this in a fizzbuzz.ts file and you're golden.
Running It
- Setup: npm init -y, npm i -D typescript ts-node @types/node. Add "scripts": { "start": "ts-node fizzbuzz.ts" } to package.json.
- Test: npm start. Watch it spit out the sequence. Tweak params for fun—try fizzbuzz({ start: 0, fizzDivisor: 1 }) (should error on divisor=1? Nah, but multiples of 1 is everything Fizz!).
- Interview Tip: In a live session, I'd verbalize the logic first: "Loop from start to end, check %3 then %5, concat strings, fallback to num." Then code it clean like this. TS adds polish without overkill.
Random Thoughts
- Pros of TS Here: Params are typed, so IDE autocompletes like a dream. Catches "str(i)" nonsense before runtime.
- Cons: Verbose validation.
- Extensions?: Maybe return an array instead of logging? Or async for big ranges? Future me, you decide.
- Why Today?: Bingeing old coding vids—FizzBuzz always sparks joy (or mild existential dread).