Intermediate Algorithm Scripting - Everything Be True

function truthCheck(collection, pre) {
let falsyArr = [false, 0, '', undefined, null, NaN]
  for (let i = 0; i < collection.length; i++) {
    for (let j = 0; j < falsyArr.length; j++) {
      if (collection[i][pre] === falsyArr[j]) {
        return false;
      }
    }
  }
  return true;  
}

Why does the code above failing to pass Test 6? i"m confused.

Hi there, I’ve edited your post for readability and included a link to the challenge.
If you wish to seek help for a particular challenge, the easiest approach is to click ‘Get Help’ and ‘Ask for Help’ on the challenge screen.
This will allow you to create a forum post which includes your full code and a link to the challenge, and also allows you to describe the issue you’re having.

1 Like

Your approach partially works but you’re not quite understanding what is required.

Iterating the array of objects with a for loop is a sound approach.
For each object in the array, you are asked to check if a particular object property has a truthy or falsy value.
This object property is stored in the variable pre and can be accessed as you have done with collection[i][pre].
All of this is good.

However, you only need to check if the value of the object property is truthy or falsy. So you don’t need to compare it explicitly with all possible falsy values, just check if it is a falsy value. The easiest way to do this:

if (!someValue) {
return false 
}

If the value of someValue is falsy it fulfills the if condition. If it’s truthy it doesn’t. So simplify your code, remove the falsyArr (and the inner for loop), and perform this simple check against collection[i][pre] inside your for loop.

This topic was automatically closed 182 days after the last reply. New replies are no longer allowed.