Skip to content Skip to sidebar Skip to footer

Determining If An Array Contains An Even Number

I'm new to javascript and am trying to get a feel for the language. Here's a function I wrote which is supposed to return true if an array contains an even number and false if it d

Solution 1:

You don't save anywhere the fact that there is an even number in your array.

You could do this

var containsEvenNums = function(numArray) {
    var containsEvenNum = false;

    numArray.forEach(function(element) {
        if(element % 2 == 0) {
            containsEvenNum = true;
        }
    });
    return containsEvenNum;
};

But this method has a disadvantage: it needs to loop through the entire array (you can't break).

I'd recommend using Array.prototype.some instead

var containsEvenNums = function(numArray) {
    return numArray.some(function(element) {
        return element % 2 === 0;
    });
};

Solution 2:

The main() method is returning falsy value because inside forEach it's checking for each value of array and then the final statement return false is run. The loop is not stopped on first match.

If you had used normal for loop it would have worked fine.

You may either use a var flag = false; in your function. Inside your forEach loop set the flag if even number is present. return that flag.

Else i would prefer:

var containsEvenNums = function(numArray) {
    return numArray.some(function(value) {
       return value % 2 ==0;
   });
}

Solution 3:

I know that this has something to do with AJAX and callback functions,

No, it doesn't; AJAX is generally a pattern describing the usage of the asynchronous call with XMLHttpRequest.

Your issue could be probably more related to Functional Programming, where you have first class functions.

In your specific case, you have two anonymous function: the first one, is assigned to containsEvenNums, and the second one is given as argument of forEach. Your return statement is inside that one, so containsEvenNums doesn't have a return statement, therefore it returns always undefined. Plus, forEach is not suggested here, because you iterate over all the array's item anyway, so if your scenario is knowing if the array contains at least one even number, is suboptimal.

You could definitely set a variable if you find an even number:

var containsEvenNums = function(numArray) {
  var containsEven = false;  

  numArray.forEach(function(element) {
    if(element % 2 == 0) {
      containsEven = true;
    }
  });

  return containsEven;
};

But as said, is suboptimal. You want probably stop as soon as you find an even number, so probably some is what you're looking for:

functionisEven(number) {
  return number % 2 === 0;
}

functioncontainsEvenNums(numArray) {
  return numArray.some(isEven);
}

As you can see, the advantage of a little more functional approach, is that you have more generic predicate functions. In that case, isEven can be used as stand-alone function, for single value:

if (isEven(userInput)) { ... }

Or for other Array's methods:

// every item of the array has to be evenvar result = numArray.every(isEven)

Or for composition.

Post a Comment for "Determining If An Array Contains An Even Number"