"Rock, Paper, Scissors" part 8


#1

This is how my code looks like, I have no idea what mistake I am making but it keep showing me the error:"Oops, try again. Your code returned 'scissors wins' instead of 'undefined' when the inputs are scissors and rock"

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

#2

Ok at least you have the console.log + function call inside the function where it should be outside and the computerChoice in the parameters has a typo in it. But I still can't see why you got the wrong output. Also better replace computerChoice and userChoice inside the function with choice1 and choice2 to avoid confusion with the global variables of the same name.


#4

i use the if command like if("userChoice") so when i use ("
It works for me when i use it


#5

I get the same error ..... hmm.....


#6

can you please print your code here , so we can understand what exactly are you talking about


#7

thank you , it works for me as well , but this is definitely a mistake .. it should be fixed


#8

All values tend to behave "truthy" or "falsy" when used in a condition and not empty strings are in the group that behaves "truthy". But unless you explicitly know what you're doing, I'd not recommend using them but sticking to your boolean values and expression that evaluate to boolean values. Even if you do know how the things behave it might be confusing to keep all this in mind.


#9

Hi haxor789,

Thank you for replying!

How do I call the console.log + function outside the function? Is it by removing the curly brackets from the very top and the bottom?

Also, can you please show me which Parameters of computerChoice has typo in it? I tried looking few times but I don't see it.

-Safura


#10

Hi safura01,

well there are only 2 parameters:

var compare = function (userChoice, computerCoice) {

and the typo is a missing "h" in computerChoice.

How do I call the console.log + function outside the function?

As inside the function is would mean between the { } after function that wrap up everything that belongs to the function. Outside the function would just mean after them so from:

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

to:

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

#11

A post was split to a new topic: 8/9 SyntaxError: Unexpected token


#12

Hi guys!!
This is my first post.. so be gentle! :smile:
A copy of my code below for anyone to compare. This works for me!

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))

Cliff


#13

This is my first post.. so be gentle!

Then you might want to have a look at this to see how you can format your code more readable:

Also pls refrain from posting code without any explanation on it.


#14

Hi @haxor789,

Thank you for your feedback, much appreciated.

Will have a read and remember it for future reference.

Cliff


#15

A post was split to a new topic: Next Steps (RPS): Rock, Paper,Scissors, Lizard, Spock


#16

you are awesome! thank you so much!