简体   繁体   English

Function 仅对数组中的第一个元素返回 true

[英]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.

相关问题 节点列表数组仅返回第一个元素 - Array of node list only returns the first element 具有对象数组的映射函数仅在 Reactjs 中返回第一个值 - map function with array of objects returns first value only in Reactjs 为什么我的javascript函数仅返回数组的第一项? - Why does my javascript function returns only first item of an array ? 为什么在我已经打印了数组的所有值的情况下只返回数组的第一个元素? - Why does it only returns only the first element of the array while i've already printed all the values of the array? 创建一个 function,给定一个数组并声明一个变量,如果变量超过数组的每个元素,则返回 true 或 false - Create a function that, given an array and declared a variable, returns true or false if the variable exceeds each element of the array 如果验证函数返回true,则关注下一个元素 - Focus next element if validation function returns true 函数仅返回第一个值 - Function returns only first value JS函数创建并推入数组 - 只有第一个元素是NaN - JS function create and push into array - only first element is NaN 如何使 function 仅更改第一个数组元素 - How to make a function change only the first array element 将数组传递给 Javascript function 仅传递第一个元素 - Passing array to Javascript function only passing first element
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM