9. Next steps Please help, code isn't working


#1


I am stuck with the finished code.


The computer says that it works, but when I play the game, all I get is the computers answer. I do not get anything that says "rock wins" or "paper wins". The rest game still works, and the course can still be completed. I would really appreciate some help.

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";
       }
   }
}
compare (userChoice, computerChoice)


#2

i am stuck at the same point.


#3

What function logs the computerChoice to the console in your code? Maybe this would log a function to the console as well. Give it a try.


#4

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

this is my code and i am getting following error.. Please help

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


#5

The error message is mentioning a problem when your code executes for choices involving "scissors" versus "rock". So look at your lines of code meant to execute when choice1/choice2 involve "rock" versus "scissors. I'm sure something will become apparent.


#6

thanks for responding back. i figured out what was wrong i was missing a statement. where i wanted to show user choice which is

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


#7

This topic was automatically closed 7 days after the last reply. New replies are no longer allowed.