I did write solutions in Javascript and Racket for a similar challenge a while ago.
Since nobody has done Javascript yet, here's a crazy implementation.
const worder = (predicate, patterner) => (prev, n) => predicate(prev, n) ? patterner(prev, n) : prev const isDivisible = d => (_, n) => n % d === 0 const isEmpty = s => s.length === 0 const append = x => (prev) => prev + x const setNumber = (_, n) => n const fizzer = worder(isDivisible(3), append('fizz')) const buzzer = worder(isDivisible(5), append('buzz')) const numberer = worder(isEmpty, setNumber) const reducer = (...worders) => n => worders.reduce( (prev, w) => w(prev, n), '' ) const fizzbuzzer = reducer( fizzer, buzzer, numberer ) for (let i = 0; i <= 100; i++) { console.log(fizzbuzzer(i)) }
Consider how easy it is to extend to print 'fazz' for multiples of 7.
'fazz'
const fazzer = worder(isDivisible(7), append('fazz')) const fizzbuzzfazzer = reducer( fizzer, buzzer, fazzer, numberer )
Here's my implementation in Racket
(define (mult-15? n) (and (mult-5? n) (mult-3? n))) (define (mult-5? n) (= (modulo n 5) 0)) (define (mult-3? n) (= (modulo n 3) 0)) (define (fizzbuzz n) (cond [(mult-15? n) "FizzBuzz"] [(mult-5? n) "Buzz"] [(mult-3? n) "Fizz"] [else n])) (define (print-list xs) (map displayln xs)) (print-list (map fizzbuzz (range 1 101)))
The print-list function is a bit redundant, since (map fizzbuzz (range 1 101)) will already print the resulting list to the console.
print-list
(map fizzbuzz (range 1 101))
Are you sure you want to hide this comment? It will become hidden in your post, but will still be visible via the comment's permalink.
Hide child comments as well
Confirm
For further actions, you may consider blocking this person and/or reporting abuse
We're a place where coders share, stay up-to-date and grow their careers.
I did write solutions in Javascript and Racket for a similar challenge a while ago.
Since nobody has done Javascript yet, here's a crazy implementation.
Consider how easy it is to extend to print
'fazz'
for multiples of 7.Here's my implementation in Racket
The
print-list
function is a bit redundant, since(map fizzbuzz (range 1 101))
will already print the resulting list to the console.