dimanche 6 décembre 2020

JavaScript change value every time a function is called

I'm making a blackjacks or 21 game, and I cannot figure out why my function value won't change, basically, this function gives you a random number, and what I want it to do is, give it a new number each time this function is called, and not the same one up to three times. (depending on the users choice.) If someone is able to solve this, I would be much appreciated, thanks.

function cardNumber(a, b) {
    var cardTotal = a + b;
    alert(`Your card numbers are ${a} and ${b}!`);
    return cardTotal;
}

var 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);

//want to give new number each time this function is called below
function moreCards(a, b) {
    alert(`Your extra card is ${a}!`);
    var cardTotal = a + b;
    return cardTotal;
}

extraCard = cardRange[Math.floor(Math.random() * cardRange.length)];
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"); //placeholder link
        //take this back to home.html ^^^
        i += 3;
    }
    //Random number doesn't change
    else if (input === "1") {
        i++;
        cardSum = moreCards(extraCard, cardSum);
    }
    //Random number doesn't change
    else if (input === "0") {
        i += 3;
    }
    else {
        alert("Wrong input, enter 1 or 0 on your keyboard!");
    }
}



Aucun commentaire:

Enregistrer un commentaire