0

I'm doing an exercise right now, and I can't figure out why this is returning undefined.

function every(array, compare) {
    var newArray = array.filter(
        function(property) {
            return compare(property);
        }
    );

    if (newArray === undefined || newArray.length == 0) {
        return false;
    }
}


console.log(every([NaN, NaN, NaN], isNaN));
// → true
console.log(every([NaN, NaN, 4], isNaN));
// → false

as you can see, I'm supposed to get true and false but instead I get undefined and undefined. Why is this the case? I think it's because I'm not understanding isNaN properly but I'm not 100% sure.

6
  • 2
    What happens if the if doesn't run? A function returns undefined by default... Commented May 3, 2016 at 0:45
  • 3
    I'm supposed to get true and false - when does your function return true? Commented May 3, 2016 at 0:47
  • 2
    Note that the return inside the filter does not return from the every function, it just determines the filter result (for a single array element). Commented May 3, 2016 at 0:49
  • return array.filter(compare).length Commented May 3, 2016 at 0:50
  • 1
    Forgot to write return true;. That’s all. Commented Oct 16 at 20:18

2 Answers 2

2
if (newArray === undefined || newArray.length == 0)
    return false;

This will trigger only if your new array is empty or undefined. Otherwise the function returns undefined. Fix that and also reverse the return of your callback function because isNaN is true when a value is not a number.

See here:

function every(array, compare) {
  var newArray = array.filter(
    function(property) {
      return !compare(property);
    }
  );
  return newArray.length == 0;
}


console.log(every([NaN, NaN, NaN], isNaN));
// → true
console.log(every([NaN, NaN, 4], isNaN));
// → false

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

1 Comment

Note that newArray === undefined is not possible, since Array.prototype.filter never returns undefined. The condition may as well just be shortened to newArray.length === 0.
1

isNaN return true when it's argument is NaN, that means that your filter function keeps NaN's but removes everything else. Its length is only 0 if the array had no NaN's in it. For such an array your function returns false:

console.log(every([4], isNaN)); // false

That seems to be the opposite of what you want. You called your function every but it acts more like it should be called some.

For everything else it returns undefined (instead of true) because you don't have a return statement in any other case. Your only return statement is inside the if statement, so if the condition isn't truthy it returns undefined. One way to fix it is to change it so that it returns the result of the comparison newArray.length === array.length:

function every (array, compare){
    var newArray = array.filter(
        function (property){
            return compare(property);  
        }
    );

    return newArray.length === array.length;

}


console.log(every([NaN, NaN, NaN], isNaN));
// → true
console.log(every([NaN, NaN, 4], isNaN));
// → false

The alternative is to negate the return value of compare inside your filter callback (See @Shomz's answer) and then return newArray.length === 0;.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.