8/9 ReferenceError: choice1 is not defined (?!?!)


#1

Thanks in advance for your help, this is the code I have so far, I cannot find the problem!

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(choice1, choice2) {
    if (choice1 === choice2) {
        return "The result is a tie!";
    }
    else if (choice1 === "rock") {
        if (choice2 === "scissors") {
            return "rock wins";
        }
        else {
            return "paper wins";
        }
    }
    else if (choice1 === "paper") {
        if (choice2 === "rock") {
            return "paper wins";
        }
        else {
            return "scissors wins";
        }
    }
    else if (choice1 === "scissors") {
        if (choice2 === "rock") {
            return "rock wins";
        }
        else {
            return "scissors wins";
        }
    }    
};
var userChoice = choice1;
var computerChoice = choice2;
console.log (compare(userChoice,computerChoice);

#2

Now the problem is "ReferenceError: choice1 is not defined"

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(choice1, choice2) {
    if (choice1 === choice2) {
        return "The result is a tie!";
    }
    else if (choice1 === "rock") {
        if (choice2 === "scissors") {
            return "rock wins";
        }
        else {
            return "paper wins";
        }
    }
    else if (choice1 === "paper") {
        if (choice2 === "rock") {
            return "paper wins";
        }
        else {
            return "scissors wins";
        }
    }
    else if (choice1 === "scissors") {
        if (choice2 === "rock") {
            return "rock wins";
        }
        else {
            return "scissors wins";
        }
    }
};
var userChoice = choice1;
var computerChoice = choice2;
console.log (compare(userChoice,computerChoice));

#3

nevermind, just skipped:

var userChoice = choice1;
var computerChoice = choice2;


#4

Ye choice1 and choice2 are parameters of the compare function and therefore only exist in this function any use outside of it will lead to an error. Also they are not necessary here either, because they get their value by the values you use in the function call:

console.log (compare(userChoice,computerChoice));

And not by assigning them to user and computerChoice.


#5

use console.log without using compare....
like this
console.log (userChoice,computerChoice);


#6

Then you'd see the value of userChoice a space sign and the value of computerChoice but this would neither solve the issue with choice1 nor let you see if compare is working, does it?