Having an issue with my function that's meant to add card values together. When I use the function with these arguments, it correctly logs 21.
score([{ suit: 'HEARTS', value: 1 }, { suit: 'SPADES', value: 11 }]) //Logs 21 as it should
However, when I try the function with the same values but in reverse order, it logs 11 instead.
score([{ suit: 'HEARTS', value: 11 }, { suit: 'SPADES', value: 1 }]) // Logs 11 but should log 21
I'm struggling to figure out how to fix this. Any guidance would be greatly appreciated. Here is the code:
let score = function (cardObject) {
let getScore = 0;
for (let i = 0; i < cardObject.length; i++) {
let cardValue = cardObject[i].value;
if (cardValue >= 10 && cardValue <= 13) {
getScore += 10;
} else if (cardValue >= 2 && cardValue <= 9) {
getScore += cardValue;
} else if (cardValue === 1) { //Ace
getScore += 11;
if (getScore + cardValue > 21) {
getScore -= 10;
}
}
}
return getScore;
}
score([{ suit: 'HEARTS', value: 11 }, { suit: 'SPADES', value: 1 }]) // Logs 11 but should log 21
score([{ suit: 'HEARTS', value: 1 }, { suit: 'SPADES', value: 11 }]) //Logs 21 as it should
score([{ suit: 'HEARTS', value: 1 }, { suit: 'SPADES', value: 1 }, { suit: 'SPADES', value: 1 }, { suit: 'SPADES', value: 10 }]) //Logs 23 but should log 13
score([{ suit: 'HEARTS', value: 10 }, { suit: 'SPADES', value: 1 }, { suit: 'SPADES', value: 1 }, { suit: 'SPADES', value: 1 }]) //Logs 13 as it should