let card_value = 0;
let number_of_cards = Number(prompt("Enter the number of cards."));
if (number_of_cards >= 2 && number_of_cards <= 5) {
let cards = [];
for (let i = 0; i < number_of_cards; i++) {
cards[i] = prompt("Enter the value of card ");
}
var dict = {
"a": (card_value + 11 > 21) ? 1 : 11,
"2": 2,
"3": 3,
"4": 4,
"5": 5,
"6": 6,
"7": 7,
"8": 8,
"9": 9,
"t": 10,
"j": 11,
"q": 12,
"k": 13
};
for (let j of cards) {
if (j == "a") {
continue;
}
else {
card_value += dict[j];
}
}
// Add the value of 'a' to card_value
card_value += dict["a"];
console.log(card_value);
}
else {
console.log("Program ends here. Please enter again.");
}
Here’s what I’ve tried. But it doesn’t works on input a,8,7 which yields 26 wheras it should’ve yield 16.
What am I missing here? Only logic, no code please.
And your code doesnt work in a few ways, I wont explain because you didnt ask and I think your trying to learn. EDIT: Meh, hints in the edit
EDIT: OH! and understand that any expressions you put in a properties value for an object is only executed when the object is created. So your value for dict.a is always going to be 1 in the current code. ANYTHING after a continue in a for loop does not execute. <= is different to <.