2

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

2
  • 1
    is reduce must ? if not why can't use every method which makes life easier :) Commented Sep 29, 2020 at 4:33
  • 2
    @Codenewbie that's probably a homework, so yes, even if every is better they still should use reduce, just to learn how to use it. Commented Sep 29, 2020 at 4:37

3 Answers 3

4

The reason your code doesn't work is that the reduce only returns the result of the comparison of the last element in the array with true. Since the last element in [false, true] is true, it returns true.

Note that your code is overly complex, you have already effectively included your conditions in the reduce (giving an initial value of true means that a zero-length array will return true). Your one issue is that the code in the reduce is incorrect, you should be checking whether all the previous values are true as well as the current one by and'ing the current value with the and of all the previous values:

const allTrue = (values) => values.reduce((curr, value) => curr && value, true);

console.log(allTrue([]));
console.log(allTrue([true]));
console.log(allTrue([false]));
console.log(allTrue([true, true, true]));
console.log(allTrue([false, true]));

Sign up to request clarification or add additional context in comments.

7 Comments

Nice handling of empty arrays 👍
@Nick I get the codes to work but I have difficulty writing them as simple as yours. Does flow-charting help with that? Thanks
@Nader.Bhr a lot of it really comes down to experience and having solved similar problems before.
@GerardoFurtado true, but then I wouldn't name my variables values, curr and value either... :-)
@Nader.Bhr generally, no. SO is for solving problems with code. Questions like that are typically better suited to codereview.stackexchange.com but be sure to read the tour there to make sure your question is appropriate before posting.
|
1

I think this can be help:

function allTrue(values) {
  return values.reduce((final, value) => final && value)
}

Comments

0

We can use every as well to find the solution to this. Below is the snippet for both every as well as reduce

var allTrue_Every = (arr) => arr.every(d => d == true)

console.log(allTrue_Every([true, true]))
console.log(allTrue_Every([true, false]))

var allTrue_Reduce = (arr) => arr.reduce((r, d) => r && d, true)


console.log(allTrue_Reduce([true, true]))
console.log(allTrue_Reduce([true, false]))

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.