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 isMultOfconstisMultOf=a=>b=>!(b%a)// from isMultOf make the two predicates we need.constisMultOf3=isMultOf(3)constisMultOf5=isMultOf(5)// and the conjuction of the two preds. above.constisMult3And5=n=>isMultOf3(n)&&isMultOf5(n)// an inclusive range function.constrange=(start,end)=>Array.from({length:(end-start+1)},(_,i)=>i+start)constfizzBuzz=(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
`
*/
:-)
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.
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.
:-)