How to get all truthy values from an array with .reduce?

Hello! How can I do that reduceToAllTrue returns true when all values are truthy.
Here it is my code. Can you explain to me why doesn’t work? How should I do instead? I got undefined.

function reduceToAllTrue(sourceArray){
    const newArr = sourceArray.reduce((acc, el) => {if (Boolean(el)){
           return acc+el;
        }else{
            Boolean(el)*0;
            return acc+el;
        }},
        true
    )
}
reduceToAllTrue([1,"w",true]);

Can you walk me through your logic? I’m not entirely sure what your intended behavior is.

What I thought : I make loop inside the sourcearr and for every element see if it truthy by Boolean(el) then add to the accumulator (which has a true value as initial value) , else turn the element into truthy value by Boolean(el)*0; ( I notice now that false*false is still false… so this part definitely wrong). I know the output should be like ‘5razmatazz’ expected to be true, if this arr is given sourceArray = [1, 2, true, "razmatazz"]

Could you explain more of what you are after? A few more examples, because I cannot tell where you get the 5 from in razmatazz.

Also, I do not think you are doing what you want here:

Boolean(el)*0;

You are not storing or using this value, so this line does absolutely nothing.

Also, why are you starting with true? Should there not be some logic to decide what you start with?

I have to check in the source arr and if every element in it is true, then reduceToAllTrue(sourceArray) should be true. I don’t know, maybe I haven’t understood the instruction even. I thought I should check every value and if they are true should add to the accumulator and if not should turn it in true and add it.

If you need the result of your reduce to be a boolean, then why are you returning acc+el instead of a boolean?

What instruction are you referring to? Can you post the original requirement?

Is it, if all elements in the array are truthy values return a single Boolean value of true?

I supposed to create a function that like the one I did in the 3rd photo (which pass the test) however, I want to do the same only using reduce (I commented out that part). The second photo is the test for this exercise.

indexTest.js - js-advanced-functions-introduction-to-map-and-reduce-lab-re-coded_istanbul_web003 WSL_ Ubuntu - Visual Studio Code 07_08_2020 18_44_38

Right, that is what I thought it was. I’m sure there are a few ways of doing it. I didn’t really think much about it but here is one version.

Not really sure how understandable this pseudo code is.

if accumulator is truthy
  if currentValue is truthy
    set accumulator to Boolean(currentValue)
  else set accumulator to false
  return accumulator
return accumulator

There might be a better way, like I said I didn’t really think much about it.

1 Like

Since we’re dealing with booleans here, we don’t even need an if/else block. We can just use a logical operator in our return.

1 Like

To answer your question, here’s one way.

function reduceToAllTrue(sourceArray){
    const result = sourceArray.reduce((acc, value) => {
     // If the `acc` is false, it means the previous
     //  value was falsy, so we always return false now 
     if (acc === false) {
        return acc;
      }

      return Boolean(value); 
    }, true)
    return result
}
reduceToAllTrue([1,"w",true, false]);

@husseyexplores He didn’t ask for a solution, he asked for help. I would suggest you blur the code.

Also, if the point was to use reduce, he can’t use every.

2 Likes