用Javascript .includes指定多个整数

时间:2019-02-23 03:36:16

标签: javascript arrays

仅在单个条件下的所有整数都与列表playerNumbers中的整数相同时,我才试图发出警报。第一组(1 && 2 && 3)有效,但是在此之上的任何内容(4 && 5 && 6)不起作用,因为4及更高范围包括1,2和3,使第一个条件成立。仅在指定了特定值然后满足条件的情况下,我才能做些什么?

     function determinePlayerWin() {
   if (playerNumbers.includes(1 && 2 && 3)) {
     alert('player win!');
   }
    else if (playerNumbers.includes(4 && 5 && 6)) {
     alert('player win!');
   }
   else if (playerNumbers.includes(7 && 8 && 9)) {
     alert('player win!');
   }
   else if (playerNumbers.includes(1 && 4 && 7)) {
     alert('player win!');
   }
    else if (playerNumbers.includes(2 && 5 && 8)) {
     alert('player win!');
   }
   else if (playerNumbers.includes(3 && 6 && 9)) {
     alert('player win!');
   }
   else if (playerNumbers.includes(1 && 5 && 9)) {
     alert('player win!');
   }
    else if (playerNumbers.includes(3 && 5 && 7)) {
     alert('player win!');
   }

 }

1 个答案:

答案 0 :(得分:4)

一系列@Override public String toString () { return new StringJoiner( " | " , Person.class.getSimpleName() + "[ " , " ]" ) .add( "name=" + name ) .add( "phone=" + phone ) .toString(); } 作为单个表达式(其中每个条件均为真)将求出最后一个值,例如

&&

相同
.includes(1 && 2 && 3)

请使用数组,并检查数组.includes(3) 的元素是否包含在every中:

playerNumbers

您还可以为每个win数组创建一个数组,以使代码更加干燥,例如:

if ([1, 2, 3].every(num => playerNumbers.includes(num))) {
  // win
}