Your code returned 'paper wins' instead of 'UNDEFINED' when the inputs are scissors and rock?


#1

As far as I can tell there is nothing different between my code and the solutions I've come across here. Would appreciate any help you could give, 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(choice1==="paper"){
if (choice2==="rock"){
return "paper wins";
}
else {
return "scissors wins";
}
}
else if(choice1="rock"){
if (choice2==="scissors"){
return "rock wins";
}
else {
return "paper wins";
}
}
else if(choice1="scissors"){
if (choice2==="paper"){
return "scissors wins";
}
else {
return "rock wins";
}
}
};

compare(userChoice, computerChoice);


#2

Yeah, this thing is literally killing me. Been tweaking it over and over and over and over.... lemme keep on trying. GAH! You have successfully burned me :expressionless:. But just change all the equal signs in your if functions to "===". Not "=".

OOH! I GOT IT! Except I fixed it like mine that I forgot what was wrong or right :sweat_smile:. But, what I can tell you and I remember is that you accidentally put the wrong things in the if and else if statements for all of them, making that "UNDEFINED" kind of thing would come out. I 100% agree that this would have stumped me too, so I would give the code which is actually YOUR tweaked 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 wins";
}
}
else if(choice1==="scissors"){
if (choice2==="rock"){
return "rock wins";
}
else {
return "scissors wins";
}
}
};


var result = compare(userChoice, computerChoice);
console.log(result)

#3

WOWZERS!!! Thanks a ton! I am obviously still working on getting it to "click" so to speak, and that just took a backseat for some reason. Lesson learned :smile: