简体   繁体   中英

Javascript function only seeing default values on global variables passed in as argument

Disclaimer: I'm fairly new to Javascript so I'm guessing I'm either doing something wrong, or misunderstanding how this works.

I have two variables with a default value of 0, which are later changed by a couple of functions.

 var userAnswerContainer = 0; var computerAnswerContainer = 0; function userInput() { userAnswerContainer = prompt("Please choose either Rock, Paper, or Scissors:").toLowerCase(); switch (userAnswerContainer) { case "rock": break; case "paper": break; case "scissors": break; default: alert(userAnswerContainer + " is not a valid answer."); userInput(); } } function computerInput() { computerAnswerContainer = Math.floor(Math.random() * 9); if (computerAnswerContainer <= 3) { computerAnswerContainer = "rock"; } else if (computerAnswerContainer >= 4 && computerAnswerContainer <= 6) { computerAnswerContainer = "paper"; } else { computerAnswerContainer = "scissors"; } } 

After these variables are changed, I'm passing them in as an argument for a separate function (on a rock, paper, scissors game) however the function seems to only be seeing the original values of the variables.

 function gameStartV2(y, c) { userInput(); computerInput(); alert(computerAnswerContainer + " / " + userAnswerContainer); /*Testing: Displays values of AnswerContainers applied by respective functions*/ if (c === y) { alert("You chose " + y + ". Computer chose " + c + ". It's a tie!"); } else if (c === "rock") { if (y === "paper") { alert("Paper covers rock, you win!"); userScore++; } else { alert("Rock smashes scissors, you lose!"); computerScore++; } compareScore(); } else if (c === "scissors") { if (y === "rock") { alert("Rock smashes scissors, you win!"); userScore++; } else { alert("Scissors cuts paper, you lose!"); computerScore++; } } else if (c === "paper") { if (y === "rock") { alert("Paper covers rock, you lose!"); computerScore++; } else { alert("Scissors cuts paper, you win!"); userScore++; } } else { alert("Error, please try again."); } compareScore(); } gameStartV2(userAnswerContainer,computerAnswerContainer); 

If I run a check on the variables inside of the argument function, I can see that they are indeed holding the values from the changing functions, however the if/else statement is still only seeing 0. What's happening here?

Jfiddle: https://jsfiddle.net/Spiderpiggie/ucuzry8p/9/

You never update your values:

function gameStartV2() {
  userInput();
  computerInput();

  y = userAnswerContainer;
  c = computerAnswerContainer;

Change it to this and it should work as intended.

In your code the script always works with the original values that were supplied to the function ( 0 and 0 ).

You don't even need to provide gameStartV2 with the values from userAnswerContainer and computerAnswerContainer

Made some improvements here below, take a peak!

 var userAnswerContainer = 0; var computerAnswerContainer = 0; var userScore = 0; var computerScore = 0; function userInput() { var userAnswerContainer = prompt("Please choose either Rock, Paper, or Scissors:").toLowerCase(); //use var to keep it within the scope of this function. switch (userAnswerContainer) { case "rock": break; case "paper": break; case "scissors": break; default: alert(userAnswerContainer + " is not a valid answer."); userInput(); } return userAnswerContainer; } function computerInput() { var computerAnswerContainer = Math.floor(Math.random() * 9); //use var to keep it within the scope of this function. if (computerAnswerContainer <= 3) { computerAnswerContainer = "rock"; } else if (computerAnswerContainer >= 4 && computerAnswerContainer <= 6) { computerAnswerContainer = "paper"; } else { computerAnswerContainer = "scissors"; } return computerAnswerContainer; //use return to set the variable to computerAnswerContainer } function gameStartV2() { c = userAnswerContainer = userInput(); //set the global and c y = computerAnswerContainer = computerInput(); //set the global and y alert(computerAnswerContainer + " / " + userAnswerContainer); /*Testing: Displays values of AnswerContainers applied by respective functions*/ if (c === y) { alert("You chose " + y + ". Computer chose " + c + ". It's a tie!"); } else if (c === "rock") { if (y === "paper") { alert("Paper covers rock, you win!"); userScore++; } else { alert("Rock smashes scissors, you lose!"); computerScore++; } } else if (c === "scissors") { if (y === "rock") { alert("Rock smashes scissors, you win!"); userScore++; } else { alert("Scissors cuts paper, you lose!"); computerScore++; } } else if (c === "paper") { if (y === "rock") { alert("Paper covers rock, you lose!"); computerScore++; } else { alert("Scissors cuts paper, you win!"); userScore++; } } else { alert("Error, please try again."); } compareScore(); } function compareScore() { alert("the score is you: " + userScore + " vs. computer: " + computerScore); } gameStartV2(userAnswerContainer,computerAnswerContainer); 

"y" and "c" are copies by value from the original values passed to the function. Remove the arguments and just use the actual variables, or create local var copies inside your gameStartV2 function, but after you've invoked your modifier functions.

function gameStartV2() {
    var y = userInput();
    var c = computerInput();
    var r;
    alert("Testing Check " + c + " / " + y);
    /*Testing: Displays values of AnswerContainers applied by respective functions*/
    if (y === c) {
        alert("You chose " + y + ". Computer chose " + c + ". It's a tie!");
    } else {
        r = y + c;
        switch (r) {
            case "paperrock":
                alert("Paper covers rock, you win!");
                userScore++;
                break;
            case "paperscissors":
                alert("Scissors cuts paper, you lose!");
                computerScore++;
                break;
            case "rockpaper":
                alert("Paper covers rock, you lose!");
                computerScore++;
                break;
            case "rockscissors":
                alert("Rock smashes scissors, you win!");
                userScore++;
                break;
            case "scissorspaper":
                alert("Scissors cuts paper, you win!");
                userScore++;
                break;
            case "scissorsrock":
                alert("Rock smashes scissors, you lose!");
                computerScore++;
                break;
            default:
                alert("Error, please try again.");
        }
    }
    compareScore();
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM