I have made this blackjack's program, and I don't know why, but the value "cardSum" does not add up in the end, basically it only adds up the first two card values and I am not sure why this is happening. My code is below if anyone wishes to help out, thanks.
function cardNumber(a, b) {
var cardTotal = a + b;
alert(`Your card numbers are ${a} and ${b}!`);
return cardTotal;
}
let cardRange = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10];
var cardOne = cardRange[Math.floor(Math.random() * cardRange.length)];
var cardTwo = cardRange[Math.floor(Math.random() * cardRange.length)];
var cardSum = cardNumber(cardOne, cardTwo);
function moreCards(a, b) {
alert(`Your extra card is ${b}!`);
var cardTotal = a + b;
return cardTotal;
}
function inputValidation(cardSum) {
var i;
for (i = 0; i < 3;) {
var input = prompt(`Which makes your card total ${cardSum}. Would you like to draw another card? (Type in 1 for yes, 0 for no, or select cancel to return to home.)`);
if (input === null) {
window.location.replace("http://stackoverflow.com");
//take this back to home.html ^^^
i += 3;
}
else if (input === "1") {
i++;
var extraCard = cardRange[Math.floor(Math.random() * cardRange.length)];
var cardSum = moreCards(cardSum, extraCard);
}
else if (input === "0") {
i += 3;
}
else {
alert("Wrong input, enter 1 or 0 on your keyboard!");
}
if (cardSum >= 22) {
i += 3;
}
}
}
function pontoonDecision(cardSum) {
var comScore = 18;
if (cardSum > comScore && cardSum < 22) {
document.write(`You got ${cardSum}, and the AI player got ${comScore}. Which means.... You win!`);
}
else if (cardSum === comScore) {
document.write(`You got ${cardSum}, and the AI player got ${comScore}. Which means.... It is a tie!`);
}
else if (cardSum >= 22) {
alert("BUST!");
document.write(`Your card number is ${cardSum}, which is above 21. Which means.... You lose!`);
}
else {
document.write(`You got ${cardSum}, and the AI player got ${comScore}. Which means.... You lose!`);
}
}
inputValidation(cardSum);
pontoonDecision(cardSum);