8/9 Console returns the game results, yet won't let me proceed


#1


https://www.codecademy.com/en/courses/javascript-beginner-en-Bthev-mskY8/1/4?curriculum_id=506324b3a7dffd00020bf661#


My code doesn't appear to be working incorrectly, as the console does present the correct game results (i.e, if I type rock, and the computer "types" paper, paper wins), but still, it won't let me proceed. This is the error message:

"Oops, try again. Make sure to call your compare function with userChoice and computerChoice as the arguments! "


var userChoice = prompt("Do you choose rock, paper or scissors?");
var computerChoice = Math.random();
if (computerChoice < 0.34) {
	computerChoice = "rock";
} else if(computerChoice <= 0.67) {
	computerChoice = "paper";
} else {
	computerChoice = "scissors";
} console.log("Computer: " + computerChoice);

var compare = function (computerChoice, userChoice) {
    if (computerChoice === userChoice) {
        return "The result is a tie!";
    } 
    else if (computerChoice === "rock") {
        
        if (userChoice === "scissors") {
            return "Rock wins!";
    } 
        else {
        return "Paper wins!";
        }
    }
    else if (computerChoice === "paper") {
        
        if (userChoice === "rock") {
            return "Paper wins!"
        }
        else {
            return "Scissors wins!"
        }
    }
    else if (computerChoice === "scissors") {
        
        if (userChoice === "rock") {
            return "Rock wins!"
        }
        else {
            return "Scissors wins!"
        }
    }
};
compare (computerChoice, userChoice)


#2

Note that the instructions when we started writing the compare() function specified local variables in the parameters: choice1 and choice2 which are what we need to use inside the function. The global variables are only to used as arguments in the function call.


#3

So my mistake was in deleting choice1 and choice2? But would my compare function still have any effect on the code as a whole without it making reference to computerChoice and userChoice? Or am I getting it wrong all again? :neutral_face:


#4

Technically, the global variables will work as expected in the function, but it is not a good practice. Local variables protect the global namespace since they are scoped to their function, not the outside.

The SCT is not exhaustive and may have a limited set of error messages so the message may not match the lesson error. Once your local variables are in place, does the code pass the lesson check?


#5

I did as you said, and reverted back to choice1 and choice2 on my compare function, and at first, for some reason, I still didn't pass the lesson, with the same error message from above being shown. I then tried calling the function using (choice1, choice2) and received a ReferenceError, as these variables hadn't been defined in the global scope. As a last resort, I once again called the function using (userChoice, computerChoice), as I had done before, and bingo! It finally worked! :smiley:
Maybe, when I reverted to choice1 and choice2, and I still didn't pass the lesson there was a bug of some sorts with the SCT? Anyhow, it worked! Thanks for the help!


#6

This topic was automatically closed 7 days after the last reply. New replies are no longer allowed.