简体   繁体   English

函数在迭代过程中返回True而不是False

[英]Function returns True instead of False over iteration

I am doing some challenges on edabit and I came across a problem with my output. 我在edabit上遇到了一些挑战,但是我的输出遇到了问题。 So i have to Create a function that takes in an array (slot machine outcome) and returns true if all elements in the array are identical, and false otherwise. 所以我必须创建一个接受数组(老虎机结果)的函数,如果数组中的所有元素都相同,则返回true,否则返回false。 The array will contain 4 elements. 该数组将包含4个元素。 Can someone explain why my code isn't working the right way. 有人可以解释为什么我的代码无法正常工作。

I have tried with other inputs and it's working fine. 我尝试了其他输入,并且工作正常。

   'use strict';

   function array_Validator(e1,e2,e3,e4)
   {
       let m_Array=[e1,e2,e3,e4];
       for(let i=0;i<m_Array.length-1;i++)
       {
           for(let x=i;x<m_Array.length-1;x++)
           {
               if(m_Array[i]!==m_Array[x+1])
               {
                   return false;
               }
               else
               return true;
           }
       }
   }


  let u_Result=array_Validator("SS","SS","Ss","Ss");

   console.log(u_Result);

So when I input Ss it shoes true instead of false. 因此,当我输入Ss时,它会显示true而不是false。

You need to move return true to the end of the function and you could use a single loop and check the first element with each others. 您需要将return true移到函数的末尾,并且可以使用单个循环并相互检查第一个元素。

 'use strict'; function array_Validator(e1, e2, e3, e4) { let m_Array = [e1, e2, e3, e4]; for (let i = 1; i < m_Array.length; i++) { if (m_Array[0] !== m_Array[i]) return false; } return true; } let u_Result = array_Validator("SS", "SS", "SS", "Ss"); console.log(u_Result); 

For this you can use Rest Parameters ...args 为此,您可以使用Rest Parameters ... args

function array_Validator(...args) {
    for (let i = 1; i < args.length; i++) {
        if (args[0] !== args[i]) return false;
    }
    return true;
}
let u_Result = array_Validator("SS","SS","Ss","Ss");
console.log(u_Result);

Note the first iteration of inner loop : you have a return statement there on the if and the else blocks. 注意内部循环的第一次迭代:在ifelse块上有一个return语句。 So the function will always returns on the first iteration of the inner loop . 因此,该函数将始终在内部循环的第一次迭代中返回。

Why the result is true for your input data "SS","SS","Ss","Ss" ? 为什么结果是true的输入数据"SS","SS","Ss","Ss"

Because in the first iteration, for x = i = 0 , the expression m_array[i] !== m_Array[x+1] evaluates to m_array[0] !== m_Array[1] and that results in "SS" !== "SS" . 因为在第一次迭代中,对于x = i = 0 ,表达式m_array[i] !== m_Array[x+1]计算结果为m_array[0] !== m_Array[1] ,结果为"SS" !== "SS" That last expression is then evaluated to false and the else block is taken returning the true value. 然后将最后一个表达式求值为false并采用else块返回true值。

There is no need to use nested loops to check if all the elements on an array are equals. 无需使用嵌套循环来检查array上的所有元素是否相等。 Now, you can use Array.every() for this situation. 现在,您可以在这种情况下使用Array.every()

Example: 例:

 function array_Validator(e1, e2, e3, e4) { let m_Array = [e2, e3, e4]; return m_Array.every(elem => elem === e1); } console.log(array_Validator("SS","SS","Ss","Ss")); console.log(array_Validator("SS","SS","SS","Ss")); console.log(array_Validator("SS","SS","SS","SS")); 
 .as-console {background-color:black !important; color:lime;} .as-console-wrapper {max-height:100% !important; top:0;} 

https://stackoverflow.com/a/24968449/11178909 https://stackoverflow.com/a/24968449/11178909

Answer provided, do not use a double for loop for this method first of all 提供答案,首先不要为此方法使用double for循环

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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