Next Steps


#1

In this version, if both players make the same choice, the game returns a tie. What if the game didn't end there but instead asked both players for new choices?

// User choice
var userChoice = prompt("Do you choose rock, paper, scissors, dog or rope ?");
// Computer Choice
var computerChoice = Math.random();
if (computerChoice < 0.20) {
computerChoice = "rock";
} else if(computerChoice <= 0.40) {
computerChoice = "paper";
} else if (computerChoice <= 0.60) {
computerChoice = "dog";
} else if (computerChoice <= 0.80) {
computerChoice = "rope"
} else {
computerChoice = "scissors";
} console.log("Computer: " + computerChoice);
console.log("User : " + userChoice);
// compare function
var compare = function (choice1, choice2) {
if (choice1 === choice2) {
console.log(userChoice);
}
else if (choice1 === "rock" ) {
if (choice2 === "scissors" || choice2 === "dog") {
return "rock wins";
} else if (choice2 === "rope") {
return "rope wins";
}
else {
return "paper wins";
}
}
else if (choice1 === "paper") {
if (choice2 === "rock" || choice2 === "rope") {
return "paper wins";
} else if (choice2 === "dog") {
return "dog wins";
} else {

    return "scissors";
    }
}

 else if (choice1 === "scissors") {
     if (choice2 === "paper" || choice2 === "rope") {
         return "scissors wins";
     } else if (choice2 === "dog") { 
         return "dog wins";
    } else {
         return "rock wins";
     }
 }
 else if (choice1 === "dog") {
   if (choice2 === "paper" || choice2 === "scissors") {
     return "dog wins";
   } else if (choice2 === "rope") {
     return "rope wins";
  }  else {
     return "rock wins";
   }
}
  else if (choice1 === "rope") {
   if (choice2 === "dog" || choice2 === "rock") {
     return "rope wins";
   } else if (choice2 === "paper") {
     return "paper wins";
  }  else {
     return "scissors  wins";
   }
}

};

compare(userChoice, computerChoice);


#2

what should i write here in the if statement that my code runs again from the userChoice?


#3

Check this https://discuss.codecademy.com/t/a-little-help-concerning-9-9-next-steps/65170 it was explained there.


#4

@bruzepam, not really? this explains how to prompt the user again if the user enters something invalid? I can know it, since i wrote the answer.

@microsurfer22225, you could divide the code into function, so you can call function again if you want to execute them again?


#5

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