- #1
shivajikobardan
- 675
- 54
- Homework Statement
- Blackjack program in javascript
- Relevant Equations
- Blackjack program in javascript
JavaScript:
let card_value = 0;
let count_of_a = 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 = {
"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") {
count_of_a++;
continue;
}
else {
card_value += dict[j];
}
}
for (let k = 0; k < count_of_a; k++) {
dict["a"] = (card_value + 11 > 21) ? 1 : 11,
card_value += dict["a"];
}
console.log(card_value);
}
else {
console.log("Program ends here. Please enter again.");
}