Search code examples
javascriptarraysnode.jsbooleanpoker

Javascript Array Test Behaves False When Is True


I am trying to make a multiplayer poker game in Node.js, and I've been having a lot of issues lately. This is a major one. This code is supposed to identify a Straight hand from an array. However, my code apparently isn't universal. I made 2 arrays as test cases and different results are produced when even just of the arrays is identified as a straight. Please help.    

Here's the code: 

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

This doesn't work for some reason. But it will work fine ONLY for the first array if you change

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

to

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

It works ONLY for the second array if you switch the variable names:

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

and run the code without further changes, I don't know why it does this. I even went as far as logging the boolean

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

to the console (in the loop, I mean), and it comes out true four times in a row (going through the first 5 indexes of the array), so I don't know why it stops at 11 for the first array and decides 11-10 isn't 1.


Solution

  • your logic is a bit hard to follow - I think the issue you're seeing is due to clearing the straight array in the if (straight.length === 2) part. Here's my shot at simplifying things:

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