Program doesn't return "_____ wins" or tie


#1

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

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 if (computerChoice === "paper") {
return "paper wins";
}
}
else if (userChoice === "scissors") {
if (computerChoice === "rock") {
return "rock wins";
}
else {
return "scissors wins";
}
}
else if (userChoice === "paper") {
if (computerChoice === "rock") {
return "paper wins";
}
else {
return "scissors wins";
}
};
};
compare(userChoice, computerChoice);


#2

Does your code pass the exercise? Because that nothing is returned, could simple be because you forgot to call your function:

compare(userChoice, computerChoice);

when making a comparison, make sure you use at least two equal signs (one means assign), look at the line:

else if (userChoice = rock) {

two problems, one equal sign, and rock is a variable here, not a string (wrap it in " to make it a string), you don't have a variable rock. I see more lines like this, that could cause a problem

I did edit your code, now it looks much better, go ahead and edit it, so you can see what i have done (inserted three backticks at beginning and end)


#3

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

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 if (computerChoice === "paper") {
return "paper wins";
}
}
else if (userChoice === "scissors") {
if (computerChoice === "rock") {
return "rock wins";
}
else {
return "scissors wins";
}
}
else if (userChoice === "paper") {
if (computerChoice === "rock") {
return "paper wins";
}
else {
return "scissors wins";
}
};
};
compare(userChoice, computerChoice);


#4

paper never wins, but the program runs and lets me pass the course.


#5

If the computer is paper or the user choice paper?


#6

Missing several semicolons after return's


#7

when user is rock, paper never wins. when user is paper, there is no tie. when user is scissors, scissors never wins. Note* I added ; after return.


#8

I used the latest version of your code i could find, but nowhere is a function call:

compare(userChoice,computerChoice)

add that to the end of your code. Without a function call, the compare function you wrote is nice, but won't do much


#9

I have that, that's not the problem.


#10

Can you post the latest version of your code?


#11

I edited the original code to show the latest version.


#12

If there is a tie, this will be returned. You currently return rock wins with your return statement, you could change this to: user wins with rock, or computer wins with paper