什么时候Javascript数组测试表现错误

时间:2018-06-02 03:12:29

标签: javascript arrays node.js boolean poker

我正在尝试在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.

2 个答案:

答案 0 :(得分:1)

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

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]))

答案 1 :(得分:0)

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张牌,则认为是直接的,无需进一步检查