mardi 28 août 2018

Why changing the "if" condition give different answers when I use === instead of !==?

The function checks if array elements are same, if they are same it should return true.

When I use the function below, it gives the correct result.

var arr = [1, 2, 3, 4];

function isUniform(arr) {
  var store = arr[0];

  for (var i = 0; i < arr.length; i++) {
    if (arr[i] !== store) {
      return false;
    }
  }
  return true;
}
console.log(isUniform(arr));

But when I use the function like this i.e; changing the if condition, it returns false

var arr = [1, 2, 3, 4];

function isUniform(arr) {
  var store = arr[0];

  for (var i = 0; i < arr.length; i++) {
    if (arr[i] === store) {
      return true;
    }
  }
  return false;

}

console.log(isUniform(arr));

Aucun commentaire:

Enregistrer un commentaire