What if choice 1 is scissors


#1


https://www.codecademy.com/courses/javascript-beginner-en-Bthev-mskY8/1/4?curriculum_id=506324b3a7dffd00020bf661#


This is the error message i get when running the code below.
"Oops, try again. Your code returned 'scissor wins' instead of 'undefined' when the inputs are scissors and paper"

I have looked all over all the topics but can't find what;s going wrong. Anyone here who could help me pleasse?


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

compare(userChoice, computerChoice);


#2

Look to your code instead. Which part of your code is responsible for that decision? The error message is a little buggy, but even so it is pinpointing where the problem is.


#3

Okay I fixed it by deleting the last block. The problem is I just don't understand why I should have done that.
Could you please explain the logic behind this move?
That would be greatly appreciated.


#4

Maybe you shouldn't have. Did that removed code have some purpose that isn't covered by anything else? If so, it should have stayed where it was, but perhaps it needed some adjusting. Or perhaps some other part needed adjusting, there are two separate places where the choices paper and scissors get handled and in both places it needs to decide that scissors is the winner.


#5

Just noticed that it isnt complete than yeah.
Will start the lesson again to build it from scratch again to get a better feeling

Thanks for the help so far


#6

Repairing what you have is good practise though - you will always have bugs, you will always need to fix things too large to rewrite!


#7

Actually, you know what. My bad.
You're picking the right choice.
But you're returning a different string from what it wants, it just wants "scissors", not "scissors wins"

A corrected error message would be:
"Oops, try again. Your code returned 'scissor wins' instead of 'scissors' when the inputs are scissors and paper"


#8

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