Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What is the most efficient way to check for a sequence of numbers in a JavaScript array?

Background

For a technical interview I was tasked with implementing a missing algorithm in JavaScript. The interviewer supplied me with some code and 18 failing unit tests that once the algorithm was successfully implemented would pass. I'm sure there is a more efficient way to have solved this problem as I tried a few different approaches during my allotted time. This way is the first way that I got to work, which for the technical test was enough but I'd like to know a better way of solving the problem.

Problem

Work out if the cards in a poker hand form a straight. (I've already ordered the hand in ascending order.)

My Solution

PokerHand.prototype._check_straight_function = function(arr) {
    var isStraight = false;
    for (var j = i = 4; i >= 0 && j > 1; i--)
        if (arr[i].value() - 1 == arr[--j].value()) {
            isStraight = true;
        } else {
            isStraight = false;
        }
    };
    return isStraight;
};

Other approaches

Things I didn't get working that I think might work faster, I'd really appreciate if someone could talk me through a working version of the below approach(es) and help me understand which is the fastest to evaluate.

  • recursive use of arr.pop().value - 1 == arr.pop().value()
  • filter the array to create a new array that contains only values where the next index (arr[++i])is the current index + 1 and then see if the new array is the same length.
  • a for loop with a break / continue to short circuit as soon as the straight ends.
like image 324
Luke Avatar asked Oct 07 '15 05:10

Luke


People also ask

What is a sequence JavaScript?

A sequence is a set of instructions executed one after the other. A source code is a collection of instructions whose sequence is the order in which it has been written. The processor will then read it in order from beginning to end, executing the code as it goes. Example. <script>

How do you find the highest and lowest numbers in JavaScript?

To get the highest or lowest number from an array in JavaScript, you can use the Math. max() or the Math. min() methods then spread the elements from the array to these methods using the spread operator ( ... ).

How do you find the largest and smallest number in an unsorted integer array JavaScript?

log(max); var min = numbers. reduce((total, value, index, array) => { if(index === 0) return array[0]; return total < value ? total : value; }); console. log(min);

What is array index in JavaScript?

JavaScript arrays are zero-indexed: the first element of an array is at index 0 , the second is at index 1 , and so on — and the last element is at the value of the array's length property minus 1 .


3 Answers

There's no need to assign a variable isStraight at all.

PokerHand.prototype._check_straight_function = function(arr) {
    for (var i = i = 4; i++) {        
        if (arr[i].value() - 1 != arr[i-1].value()) {
            return false;
        }
    };

    return true;
};
like image 174
bestprogrammerintheworld Avatar answered Sep 28 '22 15:09

bestprogrammerintheworld


Just throwing it out there, I think this one does it with the least amount of lines (3), whether or not that is "better" is subjective as it is probably less clear

    var last = arr.pop().value(), popped;
    while ((popped = arr.pop().value()) === --last);
    return popped === undefined;
like image 37
chiliNUT Avatar answered Sep 28 '22 15:09

chiliNUT


here is another way

var isOrdered=true;
[5,6,8,9].sort(function(a, b) {
  (b-a != 1)?isOrdered=false:true;
});
alert(isOrdered);

DEMO

like image 25
Nageshwar Reddy Pandem Avatar answered Sep 28 '22 15:09

Nageshwar Reddy Pandem