I dont understand


#1

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

This is what happens when i try to run my code and i don't understand.

Please can someone tell me what i am doing wrong.

This is my code and thanks:

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

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


#2

hi,

three things I noticed:

firstly, in the initial block of code you have entered code to prompt for a response but i think you have forgotten to add a line of code directly beneath that shows the choice the user made in the command window:

console.log("User:" + " " + userChoice);

// Question to the user

var userChoice = prompt("Do you choose rock, paper or scissors?");
console.log("User:" + " " + userChoice);

secondly, i notice you have entered choice2 twice:

// Compare User and Computer's choice

else if (choice2 === "rock") {
if (choice2 === "scissors") {
return "rock wins";
}

tweak code
else if (choice1 === "rock") {
if (choice2 === "scissors") {
return "rock wins";

lastly, can I suggest you also try tweaking your last else if too:

else if (choice1 === "scissors") {
if (choice2 === "paper") {
return "scissors wins";
}
else {
return "rock wins";
}
try tweaking to this instead:

else if (choice1 === "scissors"){
if (choice2 === "rock") {
return "rock wins";
}
else {
return "scissors wins";
}

then when you call the function and compare hopefully it will work

Noteworthy: I called my function slightly differently to yourself:

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

//call function

compare(userChoice,computerChoice);

Testing: check if correct answer appears ...answers -= yes they are correct outputs.

output1:

User: rock
Computer: scissors
"rock wins" // rock smashes scissors
output2:

User: paper
Computer: scissors
"scissors wins" // scissors cuts paper

output3:
User: scissors
Computer: paper
"scissors wins"

hope that is of some help and sorry if I have not explained it clearly as I am not that far ahead of you myself