Step 8


#1

How or where do I call the functions userChoice and computerChoice in step 8? Thanks.


#2

userChoice and computerChoice are variables so you don't call them at all. What you call is the compare function (anywhere after it's declaration) with uC and cC as it's arguments (the values in () )


#3

so this is what my code looks like at the moment and i'm not sure what's wrong..

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";
}
}
}


#4

You're calling the variables here:

var compare = function (choice1, choice2) {

and returning new values to the function using the "return" statements.

Remember also, in order to call a FUNCTION you'll need something like this at the end of your code:

compare(userChoice, computerChoice);

#5

Thanks lolman. I've edited my code to this but I still cant clear it. I guess the main point is that I dont understand why we're calling these 2 variables again even though we have choice1 and choice2?
}
}
compare(userChoice, computerChoice);
}


#6

Your problem looks like it's here:

}
}
compare(userChoice, computerChoice);
}

It should be this:

}
}
}
compare(userChoice, computerChoice);

#7

by calling the compare function:

compare(userChoice, computerChoice);

you assign the values (in userChoice and computerChoice) to the parameters (choice1 and choice2):

var compare = function (choice1, choice2) {


#8

Unfortunately I'm still seeing "SyntaxError: Unexpected token ILLEGAL".


#9

I don't understand what's wrong... I can enter paper or scissors or rock but always pops up error: rock 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(userChoice,computerChoice) {
if (userChoice===computerChoice) {
return "The result is a tie!";
}
else if (userChoice===rock) {
if (computerChoice===scissors) {
return "rock wins";
}
else {
return "paper wins";
}
}
else if (userChoice===paper) {
if (computerChoice===rock) {
return "paper wins";
}
else {
return "scissors wins";
}
}
};
compare (userChoice,computerChoice);


#10

Why is it that we cant just define computer & user choice like this:
var computerChoice = "choice1";
var userChoice = "choice2";


#11

Thanks ishu_nair, i made one more mistake i didn't write quotes...


#12

You had a semi colon after your final } so even when I removed it, the overall code had an error


#13

So this is my code

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 win");
}
}else if (choice1="scissors"){
if (choice2="rock"){
return ("rock wins");
}else{
return("scissors wins");
}
}
};

compare(userChoice, computerChoice);

and the error I get is"Oops, try again. Your code returned 'rock wins' instead of 'undefined' when the inputs are scissors and paper"

Can someone help please? :slightly_smiling:


#14

"rock", "paper", and "scissors" are strings and need to be in quotes throughout. You didn't put them in quotes in parts of your code.