I was trying to solve the following question:
Write a function that returns true if all array elements are true. Use reduce
The is the solution I made up
function allTrue(values) {
if ( values[0] === false) return false
if (values.length === 0) return true
let result = values.reduce( (final, value) => final = value === true, true)
return result ? true : false
}
My question is, whether it is possible to move the external conditions into the reduce method's body or not.
In other words, if I was required to use a single reduce method to solve it, can I implement it?
I tried manipulating the final in the reduce, but it is modified later in next iterations
For instance the following test should return false but returns true
allTrue([false, true])
Any explanation would be appreciated, thank you
reducemust ? if not why can't useeverymethod which makes life easier :)everyis better they still should usereduce, just to learn how to use it.