简体   繁体   中英

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!

It is because of your return false statement, you should put it outside the loop and remove the else statement:

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;
    }
  }
  return false;
}

Explanation: When the greetings argument is not equal to the first element, 'hello' , then the code would execute the else statement, which returns false and stops the function execution.

Your return statements are breaking out of the function. If you remove the return statements, it'll loop through the whole array (although it looks like you're not actually doing anything, so I don't know how you'll know that).

If you simply want to return true if greetings is in the array, then this is what you're looking for:

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;
    } 
   }
    return false;
}

Note that the return false has been moved to outside of the loop. This way it'll return true as soon as greetings is found, otherwise it'll finish the loop and then return false.

I add notes in your code and change the wrong part. Remove the else block and put the return false; out of the for loop. This will make a full for loop. Only the condition of if statement is true ,the for loop will return . And if the for loop is over without a return , the return false; statement below will be executed.

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;
        }
        /* if you add a else block here. it means you want your 'else condition' to be checked in every single loop. Since 'if statement has a 'return' and 'else' statement has a 'return'. So if there is a 'else' here, no matter what condition you add , your for loop can only be executed once.  */
    }
    return false; // only the for loop is over and no return. this statement will be executed.
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM