Background
For a technical interview, I was instructed to implement the missing algorithm in JavaScript. The interviewer provided me with some code and 18 failed unit tests, which, as soon as the algorithm is successfully implemented, will pass. I am sure there is a more efficient way to solve this problem, since I tried several different approaches during my allotted time. This way is the first way I have to work, which was enough for a technical test, but I would like to know the best way to solve the problem.
Problem
Work out if the cards in the poker hand form a straight line. (I already ordered the hand in ascending order.)
My decision
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
, , , -, , , - , .
arr.pop().value - 1 == arr.pop().value()
filter
, , (arr[++i])
+ 1, , .- a
for loop
break / continue
, .