0

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);
hhh
  • 9
  • 4
  • 2
    `cardSum` is a global variable. But you are passing it in as an argument. When you do that, and you change the argument passed into the functions, it will only affect the variable in the method, not the global one. – Taplar Dec 04 '20 at 22:08
  • Does this answer your question? [What is the scope of variables in JavaScript?](https://stackoverflow.com/questions/500431/what-is-the-scope-of-variables-in-javascript) – Taplar Dec 04 '20 at 22:15

1 Answers1

0

The problem is that you have this cardSum:

var cardSum = cardNumber(cardOne, cardTwo);

this cardSum:

function inputValidation(cardSum) {

and this cardSum:

var cardSum = moreCards(cardSum, extraCard);

You should rename them differently, because they mean different things. If you can't find them different own names, then rename them to cardSum, _cardSum and __cardSum.

This also challenges you to recognize which of the three variables are you using where. For the sake of good practices and clean code you should never name two variables the same if they are visible in the same scope.

cape_bsas
  • 638
  • 5
  • 13
  • I wasn't actually sure what parameters I put in the functions, what would be appropriate? – hhh Dec 05 '20 at 12:46
  • when I tried changing the variable names, it broke the whole program for me... – hhh Dec 05 '20 at 13:00