Javascript 数组测试在为真时表现为假

Javascript Array Test Behaves False When Is True

我正在尝试在 Node.js 中制作一款多人扑克游戏,但最近我遇到了很多问题。这是一个主要的。此代码应该从数组中识别直手。但是,我的代码显然不是通用的。我制作了 2 个数组作为测试用例,即使只有一个数组被识别为直线,也会产生不同的结果。请帮忙。

代码如下:

    var arr = [9,1,2,11,8,12,10];   // first array
    var arr2 = [9,1,8,4,5,3,2];     // second array
    var straight = [];

    // Removes duplicate elements in an array
    /* example:
    array = removeDuplicates(array)
    */
    function removeDuplicates(arr){
        let unique_array = []
        for(let i = 0;i < arr.length; i++){
            if(unique_array.indexOf(arr[i]) == -1){
                unique_array.push(arr[i])
            }
        }
        return unique_array
    }

    //Sorts the array
    arr.sort(function(a,b){return b-a});

    //Removes duplicates
    arr = removeDuplicates(arr);

    // Displays sorted and cleaned up array
    console.log(arr)

    /*Basic translation: loops through the array
    and if the difference between the a term and
    the term after it is 1, it will append it to the 
    array 'straight'. It will break if the difference 
    is greater than 1. Then it will remove those elements
    from the original array and retry to append consecutive
    elements in the 'straight' array.
    */
    for (var i=1; i<arr.length+1; i++) {
      if (arr[i-1] - arr[i] === 1) {
        straight.push(arr[i-1],arr[i]); // error occurs at this line
      } else if (arr[i-1] - arr[i] > 1){
        break; }

        if (straight.length === 2) {
          arr.splice(arr.indexOf(straight[0]),1)
          arr.splice(arr.indexOf(straight[1]),1)
          straight = [];

          for (var i=1; i<arr.length; i++) {
            if (arr[i-1] - arr[i] === 1) {
              straight.push(arr[i-1],arr[i]);
            }
          }
        }
    };

    // There are duplicates and I don't know why sometimes
    straight = removeDuplicates(straight)
    console.log(straight);

由于某些原因,这不起作用。但如果你改变

,它只对第一个数组有效
straight.push(arr[i-1],arr[i]); 

straight.push(arr[i-1],arr[i],arr[i]);

如果切换变量名称,它仅适用于第二个数组:

  var arr2 = [9,1,2,11,8,12,10];   // first array
  var arr = [9,1,8,4,5,3,2];     // second array

和运行代码没有进一步修改,我不知道为什么会这样。我什至记录了布尔值

    arr[i-1] - arr[i] === 1

到控制台(在循环中,我的意思是),它连续四次结果为真(通过数组的前 5 个索引),所以我不知道为什么它在 11 处停止对于第一个数组并决定 11-10 不是 1.

您的逻辑有点难以理解 - 我认为您看到的问题是由于清除了 if (straight.length === 2) 部分中的 straight 数组。这是我简化事情的方法:

const isStraight = a => { 
  const uniq = a.filter((val, idx) => a.indexOf(val) === idx);
  uniq.sort((a, b) => a-b); 
  const tries = uniq.length - 4;
  for (var i=0; i<tries; i++) {
    if (uniq[i + 4] - uniq[i] === 4) {
      return true;
    }
  }
  return false; 
}

console.log(isStraight([9,1,2,11,8,12,10]));
console.log(isStraight([9,1,8,4,5,3,2]));
console.log(isStraight([2,5,4,3,6,8,7]));
console.log(isStraight([2,5,4,3,6,8,7,10]));
console.log(isStraight([2,5,2,4,7,3,6,8,8,8,8,7]));
console.log(isStraight([1,2,3,4,6,7,8,9,11,12,13,13]))

let arr = [9,1,2,11,8,12,10];

function checkStraight(arr) {
  let answer = [];
  
  if(arr.length < 5)
    return false;

  arr = [...new Set(arr)];
  arr.sort(function(a,b){return b-a});

  for(let index=0; index < arr.length; index++){
    if(answer.length === 5) break;

    if(answer.length === 0){
      answer.push(arr[index])
    }

    if(answer[answer.length-1] - arr[index] === 1){
     answer.push(arr[index]);
    }else{
      answer = [];
      answer.push(arr[index])
    }
  } 
  
  return answer
}

console.log(checkStraight(arr));

你可以尝试运行通过代码,应该很简单。基本上我们不是比较自己数组中的元素,而是比较两个数组,并有条件地将匹配的直牌推入新数组

**假设:**因为我们在玩扑克,假设一旦找到 5 张连续的牌,就认为是顺子,不需要进一步检查