[英]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.
但是,如果这是我的js知识中的根本性或简单性鸿沟,我宁愿得到一个解释,为什么我可以早日而不是稍后开始养成良好的习惯。
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. 似乎每次我运行此for循环时,它仅检查第一个数组
hello[0]
,然后似乎中断。 How can I stop this from happening? 我该如何阻止这种情况的发生? I tried using
continue;
我尝试使用
continue;
after return true but that didn't fix it either. 返回true后,但这也无法解决。 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: 因为您的
return false
语句,您应该将其放在循环之外并删除else
语句:
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. 说明:当
greetings
参数不等于第一个元素'hello'
,代码将执行else
语句,该语句返回false
并停止函数执行。
Your return
statements are breaking out of the function. 您的
return
语句脱离了该功能。 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). 如果删除
return
语句,它将遍历整个数组(尽管看起来您实际上并没有做任何事情,所以我不知道您怎么知道)。
If you simply want to return true if greetings
is in the array, then this is what you're looking for: 如果您只想在数组中添加
greetings
返回true,那么这就是您要查找的内容:
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. 请注意,返回false已移至循环外部。 This way it'll return true as soon as
greetings
is found, otherwise it'll finish the loop and then return false. 这样,一旦找到
greetings
,它将返回true,否则它将完成循环,然后返回false。
I add notes in your code and change the wrong part. 我在代码中添加了注释,并更改了错误的部分。 Remove the
else
block and put the return false;
删除
else
块,并将return false;
out of the for
loop. 退出
for
循环。 This will make a full for
loop. 这将形成一个完整的
for
循环。 Only the condition of if
statement is true
,the for
loop will return
. 只有
if
语句为true
的条件, for
循环才会return
。 And if the for
loop is over without a return , the return false;
如果
for
循环结束且没有返回值,则return false;
否则, 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.
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.