samedi 6 avril 2019

JavaScript: Test Array Elements for Its Divisibility Into Another Input Argument Else Return True Question

I have the following question:

// Write a function that returns true if all integers in an array are factors of a number, and false otherwise.

I tried the code below:

function checkFactors(factors, num) {

  for (let i=0; i<factors.length; i++){
    let element = factors[i];
      console.log(element)

    if (num % element !== 0){
      return false 
    }
    else {
      return true
    }
  }
}



checkFactors([1, 2, 3, 8], 12) //➞ false

My solution returns true which is wrong. I know it's the else statement that's messing it up. But I want to understand why the else statement can't go there.

Aucun commentaire:

Enregistrer un commentaire