### re: How to approach solving a challenge during a coding interview VIEW POST

re: You bet you can leave comments David and what a great contribution. Regarding TDD I think it can definitely be something to ask at the top of the...

One last visit to fizzBuzz re: making output testable and creating small testable pure functions.

Here, I've created a reuseable predicate function isMultOf, which is then used it to make two further predicate functions isMultOf3 and isMultOf5. These are then combined to make the conjunction predicate isMult3And5. Finally, a range function generates the array of integers required.

``````
// curried isMultOf
const isMultOf = a => b => !(b % a)
// from isMultOf make the two predicates we need.
const isMultOf3 = isMultOf(3)
const isMultOf5 = isMultOf(5)

// and the conjuction of the two preds. above.
const isMult3And5 = n => isMultOf3(n) && isMultOf5(n)

// an inclusive range function.
const range = (start, end) => Array.from({length: (end - start + 1)}, (_, i) => i + start)

const fizzBuzz = (s = 1, e = 100) =>
range(s, e)
.reduce((p, c) =>
isMult3And5(c)
? p + 'FizzBuzz\n'
: isMultOf3(c)
? p + 'Fizz\n'
: isMultOf5(c)
? p +'Buzz\n'
: p + c + '\n', '')

console.log(fizzBuzz(1, 30))

/*
=> `1
2
Fizz
4
Buzz
Fizz
7
8
Fizz
Buzz
11
Fizz
13
14
FizzBuzz
16
17
Fizz
19
Buzz
Fizz
22
23
Fizz
Buzz
26
Fizz
28
29
FizzBuzz
`
*/

``````

:-)

code of conduct - report abuse  