[英]Function only returns true for first element in array
I am trying to write a function that will return true
if either 'purple' or 'magenta' elements are present in an array.我正在尝试编写一个 function 如果数组中存在“紫色”或“洋红色”元素,它将返回
true
。 However my code will only return true
when either purple or magenta are the first item in the array:但是,只有当紫色或洋红色是数组中的第一项时,我的代码才会返回
true
:
function containsPurple(arr) {
for (let i = 0; i < arr.length; i++) {
if (arr[i] === 'purple' || arr[i] === 'magenta') {
return true;
}
return false;
}
}
So for example, containsPurple(['black', 'magenta', 'yellow'])
returns false
, however containsPurple(['magenta', 'black', 'yellow'])
returns true
.例如,
containsPurple(['black', 'magenta', 'yellow'])
返回false
,但是containsPurple(['magenta', 'black', 'yellow'])
返回true
。 Where am I going wrong?我哪里错了?
Consider your condition:考虑你的情况:
if (arr[i] === 'purple' || arr[i] === 'magenta') {
return true;
}
return false;
Let's assume that arr[i] === 'orange'
.让我们假设
arr[i] === 'orange'
。 Does it match your if-condition?它是否符合您的 if 条件? No. So it continues to the next line, which is
return false
.不,所以它继续到下一行,即
return false
。 Once a function encounters a return statement, the function is completed and the return value is returned.一旦function遇到return语句,则function完成,返回返回值。 Never will your loop go to the next iteration, because in the first iteration it will always encounter a
return
statement (it be either return true
or return false
).你的循环 go 永远不会到下一次迭代,因为在第一次迭代中它总是会遇到一个
return
语句(它要么是return true
要么是return false
)。
To fix this you can move the return false
outside of your loop.要解决此问题,您可以将
return false
移到循环之外。 This way it will keep looping until any iteration matches the if
condition.这样,它将一直循环,直到任何迭代与
if
条件匹配。 If not, it will exit the loop and continue to the return false
and finish the function with the return value false
.如果不是,它将退出循环并继续
return false
并以返回值false
完成 function 。
function containsPurple(arr) { for (let i = 0; i < arr.length; i++) { if (arr[i] === 'purple' || arr[i] === 'magenta') { return true; } } return false; } console.log('black, yellow', containsPurple(['black', 'yellow'])); console.log('black, magenta, yellow', containsPurple(['black', 'magenta', 'yellow'])); console.log('magenta, black, yellow', containsPurple(['magenta', 'black', 'yellow']));
That being said, in your case you can simplify your code using Array.prototype.some()
:话虽如此,在您的情况下,您可以使用
Array.prototype.some()
简化代码:
function containsPurple(arr) { return arr.some(element => element === 'purple' || element === 'magenta'); } console.log('black, yellow', containsPurple(['black', 'yellow'])); console.log('black, magenta, yellow', containsPurple(['black', 'magenta', 'yellow'])); console.log('magenta, black, yellow', containsPurple(['magenta', 'black', 'yellow']));
function containsPurple(arr){
for(let i = 0; i < arr.length; i++){
if(arr[i] === 'purple' || arr[i] === 'magenta'){
return true;
}
// you're returning false after checking only the first value
// return false;
}
// return false once you checked the entire array
return false;
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.