mardi 4 mai 2021

Why does !== work in my facotrchain function, but not === in Javascript

Why is it that the following code works when I use !== in the conditional statement,

function factorChain(arr) {
  //iterate through array
  for(var i = 0; i < arr.length; i++){
    if(arr[i+1] % arr[i] !== 0){
      return false;
    } 
  }
  //check to see if previous element is a factor of current element
  //if so, return true
  return true;
}
console.log(factorChain([2, 4, 6, 7, 12]));

but when I change it to === as in the second section of code, it does not work.

function factorChain(arr) {
    //iterate through array
  for(var i = 0; i < arr.length; i++){
    if(arr[i+1] % arr[i] === 0){
      return true;
    } 
  }
  //check to see if previous element is a factor of current element
  //if so, return true
  return false;
}
console.log(factorChain([2, 4, 6, 7, 12]));

Aucun commentaire:

Enregistrer un commentaire