For some reason the second for loop is starting at 1. I do realize that first for loop i starts at one. That’s is meant to be. However, even though j for loop says to start at 0, it starts at 1 anyways.
var findWinners = function (playersRay) {
var players = playersRay;
var results = new Array();
//getdealers dealers hand and info
var dealerHand = players[0]
var dealerScore = dealerHand.getScore()
var dealerBust = dealerScore > 21 ? true : false;
//loops through all players; skips dealer (array position 0)
var numPlayers = players.length;
for (var i=1; i<numPlayers; i++) {
//loops through all the players hands.
//player might have more than 1 hand if he splits his cards
var player = players[i];
var numHands = player.length;
results[i] = new Array();
for (var j=0; j<numHands; j++)
var handScore = player[j].getScore();
if (handScore > 21) {
results[i][j] = false;
}
else if (dealerScore > 21) {
results[i][j] = true;
}
else if (handScore > dealerScore) {
results[i][j] = true;
}
else {
results[i][j] = false;
}
}
return results;
}
It returns this: [undefined, [undefined, true]]
It should return this: [undefined, [true]]
Just in case you want to know. A sample playersRay is: [Object, [Object]]
The object has information about the dealer’s or player’s blackjack hand.
Link to full code: https://docs.google.com/open?id=0BxvwY0fUFc3aMTdTOXU0b0ttamM
In Javascript, when you omit the curly braces around a statement, it only runs the first line. The behavior appears to omit only the first index, when I suspect there’s only two. So if you add some more to iterate, you should notice it’s actually just running the last index.
For example:
http://jsfiddle.net/MMQD8/
Gives:
MDN explains it thusly: