lundi 10 octobre 2016

For loop with if statement not iterating over entire array

I feel embarrassed asking such a fundamental question. But if it is a fundamental or simple gap in my js knowledge, I would rather get an explanation as to why so I can start making good habits sooner rather than later.

I have a function that takes in a string as an argument and compares it with array values.

function validateHello(greetings){
var hello = ['hello','ciao','salut','hallo', 'hola', 'ahoj','czesc'];
  for(var i = 0; i < hello.length; i++){
    if(greetings === hello[i]){ 
      return true;
    }else{ 
      return false;
    }

   }

}

It appears that every time I run this for loop, it only checks the first array hello[0] and then it appears to break. How can I stop this from happening? I tried using continue;after return true but that didn't fix it either. I feel like I should know this, but I am totally brainfarting and cannot figure out why. Thanks!

Aucun commentaire:

Enregistrer un commentaire