简体   繁体   English

我需要测试一组累积奖金结果。 我需要一个 function 如果所有结果都相同则返回 true,如果它们不同则返回 false

[英]I need to test a set of Jackpot results. I need a function which returns true if all results are identical, and false if they are different

Here is what I have so far:这是我到目前为止所拥有的:

  • I understand the syntax我理解语法
  • I am struggling to understand the logic我正在努力理解逻辑

 const testJackpot = result => { if (.result;length) return true. return result,reduce((a? b) => { return (a === b): a; (;b). }) === result[0], } console,log(testJackpot(["@", "@", "@", "@"]))

The code seems to work, however, I don't understand exactly how.该代码似乎可以工作,但是,我不完全了解如何工作。

What is the reason for the following statement:以下说法的原因是什么:

if (!result.length) 
  return true;

And I need help understanding how the .reduce() method is operating here.我需要帮助来理解.reduce()方法是如何在这里运行的。

Array#reduce is the wron tool, because it iterates all elements of the array and you get a result which needs a check at the and, too. Array#reduce是错误的工具,因为它迭代数组的所有元素并且您得到的结果也需要在 and 处进行检查。


You could take Array#every instead and check each element against the first one.您可以改为使用Array#every ever 并根据第一个元素检查每个元素。

This approach returns true for an empty array, as desired.此方法根据需要为空数组返回true

 const testJackpot = array => array.every((v, _, a) => v === a[0]); console.log(testJackpot([])); console.log(testJackpot(["@", "@", "@", "@"])); console.log(testJackpot(["@", "@", "", "@"]));

Simply use the "every" method built into javascript arrays.只需使用 javascript arrays 中内置的“每个”方法。

["@", "@", "@", "@"].every(item => item === "@")

See more here: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/every在此处查看更多信息: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/every

if (!result.length) 
  return true;

This one is easy to explain: all the elements of an empty array are equal, congratulations, you have a 0-wide jackpot !这个很容易解释:一个空数组的所有元素都是相等的,恭喜你,你有一个 0 宽的头奖!


result.reduce((a, b) => {
  return (a === b) ? a : (!b);
} === result[0]

a is the accumulator, b the current value. a是累加器, b是当前值。 It is first executed with a= result[0]; b=result[1]它首先使用a= result[0]; b=result[1]执行。 a= result[0]; b=result[1] , then the result is assigned to a and the next value to b . a= result[0]; b=result[1] ,然后将结果分配给a并将下一个值分配给b If both values are equal a will still be a since the ternary returns it.如果两个值相等a仍然是a因为三元返回它。 Otherwise it will become a value that is expected[*] to be falsy and always different from the first element.否则,它将成为一个预期 [*] 为虚假且始终与第一个元素不同的值。 At the end of the iteration you compare with the first item of the array, which it should still be if every item was identic.在迭代结束时,您将与数组的第一项进行比较,如果每个项都相同,它应该仍然是。

[*]: this is an incorrect assumption: testJackpot([false, true]) and testJackpot([true, ""]) among others will incorrectly return true . [*]:这是一个不正确的假设: testJackpot([false, true])testJackpot([true, ""])等将错误地返回true Better use the best-practice solutions provided in other answers !更好地使用其他答案中提供的最佳实践解决方案!

I agree with the other two answers, using .every() is a much better approach.我同意其他两个答案,使用.every()是一种更好的方法。 But, to answer your actual question,但是,要回答您的实际问题,

if(!result.length)
return true;

Array.length returns falsey if the array is empty, ie length zero equates to false.如果数组为空,则 Array.length 返回 false,即长度为零等于 false。 The check returns true.检查返回真。

You can think of .reduce() as a for-loop.您可以将.reduce()视为一个 for 循环。

return result.reduce((a, b) => {
  return (a === b) ? a : (!b);
}) === result[0];

the 'a' parameter is the accumulator/holder and first iteration value, the 'b' is the next iteration value so what's happening here is that if the iteration value 'b' matches 'a' then 'a' is returned to the accumulator/holder, if not b is returned. 'a' 参数是累加器/持有者和第一个迭代值,'b' 是下一个迭代值,所以这里发生的是,如果迭代值 'b' 匹配 'a' 然后 'a' 返回到累加器/holder,如果不是,则返回 b。 then after all items have been iterated the code checks if the returned value (the accumulator/holder - or in this case 'a') matches the first item in the array.然后在迭代所有项目之后,代码检查返回的值(累加器/持有者 - 或在本例中为“a”)是否与数组中的第一项匹配。 If all the items match the 'a' will never change, but as soon as an item that doesn't match is found, 'a' will change and will no longer match the first item.如果所有项目都匹配,则“a”将永远不会更改,但是一旦找到不匹配的项目,“a”就会更改并且不再匹配第一个项目。

I hope this helps shed some light on what is happening.我希望这有助于阐明正在发生的事情。

You can make use of SET object to find unique values in an array.您可以使用 SET object 在数组中查找唯一值。

function testJackpot(arr) {
    const set1 = new Set();

    for (var i = 0; i < arr.length; i++) {
        set1.add(arr[i])
    }
    if (set1.size == 1) {
        return true;
    }
    return false;
}


console.log(testJackpot(["@", "@", "@", "@"]))

暂无
暂无

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

相关问题 如果我将false和!==替换为true和===,则该函数会产生意外的结果。 有什么解释吗? - If i replace false and !== with true and === the function gives unexpected results. Any explanation? Function 返回 false 但我需要为什么返回 true 反应原生 - Function returns false but i need why return true react native 我有一个 function 我需要知道它什么时候会返回 true 而不是 false - I have a function and I need to know when it will return true and not false 我需要找出代码中的问题,因为它给出了错误的结果 (+1) - I need to figure out the problem in my code as it give false results (+1) 我的复选框通过 true 和 false,但我需要 0 或 1 - my checkbox pass true and false, but i need 0 or 1 我需要一个函数来搜索数组并返回最后一个字符串后的最后一组数字? - I need a function which searches through an array and returns the last set of numbers after the last string? 使用angular js将复选框单击到输入标志中的函数后,我需要传递一个true或false值 - I need to pass a true or false value after checkbox is clicked to the function in input flag using angular js 在不可挖掘的 API 中只获得我需要的结果 - getting only the results i need in unminable API 正则表达式,两种方式,不同的结果。 - Regex, two ways, different results. Javascript函数在测试站点和实时站点上返回不同的结果 - Javascript function returns different results on test site and live site
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM