Skip to content

Instantly share code, notes, and snippets.

@edwingustafson
Last active April 3, 2025 22:04
Show Gist options
  • Save edwingustafson/dc8199bd4dec09df03243422b960d467 to your computer and use it in GitHub Desktop.
Save edwingustafson/dc8199bd4dec09df03243422b960d467 to your computer and use it in GitHub Desktop.
FizzBuzz is RxJS
#!/usr/bin/env node
const fizz = "Fizz";
const buzz = "Buzz";
const fizzbuzz = `${fizz}${buzz}`;
const Rx = require('rxjs/Rx');
Rx.Observable.range(1,100)
.map(n => n % 15 === 0 ? fizzbuzz : (n % 3 === 0 ? fizz : (n % 5 === 0 ? buzz : n)))
.subscribe(console.log);
@Babali42
Copy link

Babali42 commented Apr 3, 2025

I really like it !
A new way with rxJs 6 or. above could be

const { range } = rxjs; // i use a cdn, don't care about this
const { map } = rxjs.operators; // i use a cdn, don't care about this

const fizz = "Fizz";
const buzz = "Buzz";
const fizzbuzz = `${fizz}${buzz}`;

range(1, 100).pipe(
    map(n => n % 15 === 0 ? fizzbuzz : (n % 3 === 0 ? fizz : (n % 5 === 0 ? buzz : n)))
).subscribe(console.log);

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment