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


#1

Hey All!

Been struggling for a long while.. I read through some similar comments but was wondering if any one had any feedback for me [just started courses/any learning ~5 hours ago]

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 (userChoice, computerChoice) {
    if (userChoice === computerChoice) {
        return "The result is a tie!";
        
    } if (userChoice === "rock");
       else if (computerChoice === "scissors") {
            return "rock wins";
        } else {
            return "paper wins";
    
    } if (userChoice === "paper");
       else if (computerChoice === "rock") {
            return "paper wins";
        } else {
            return "scissors wins";
    }
    
    if (userChoice === "scissors");
       else if (computerChoice === "rock") {
            return "rock wins";
        } else {
            return "scissors wins";
    }
};

console.log (compare (userChoice, computerChoice))

#2

@leonhard_wettengmx_n would you mind taking a look? I've only bee on this site for all of 45 minutes, but you seem to have an eye for this!


#3

@brookegerstein,
Please read
http://www.codecademy.com/forum_questions/559dcb4be39efe550f00006b
concentrate on

  • function talk , parameter, argument
  • creation of the compare function with parameter choice1 and choice2
  • the truth table and required return strings
  • the build of the IF ELSE-IF ELSE-IF statement structure

#4

got it, thank you! not sure I've grasped the usage of curly brackets


#5

The curly-brackets-{ }
with which you encapsulate code ( which should stand together )
is seen by the Javascript-Interpreter
as the encapsulation of a =code-block=

var aFunction = function() {
       //function =code-block=
        //also called FUNCTION-BODY
       };

or

if ( condition ) {
    //IF code-block
}
else {
   //ELSE code-block
}

#6

Got a little bit confused, because in the exercise 6., 7. you should give a return value.
In exercise 8 there should be no return value ... :frowning:

br,
Niko


#7

hope my code can help you debugging yours

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 "scissors wins"}
    else
    { return "rock wins" }
}

};
console.log(compare(userChoice,computerChoice));