Rock, Paper, Scissors in JavaScript - javascript

I'm on a codecademy lesson (found here) but keeps telling me "Your code returned 'rock wins' instead of 'paper wins' when the inputs are paper and rock", why? It should be correct. Since it's talking about 'rock wins' then it's talking about rock vs scissors. So why is it saying that "instead of paper wins" when there isn't even paper involved in the only outcome of 'rock wins'?
var compare = function (choice1, choice2) {
if (choice1 === choice2) {
return("The result is a tie!");
}
if (choice1 === "rock") {
if (choice2 === "scissors");
} else {
return ("rock wins");
}
if (choice1 === "paper") {
if (choice2 === "rock");
} else {
return ("paper wins");
}
if (choice1 === "paper") {
if (choice2 === "scissors");
} else {
return ("scissors wins");
}
};

Look at your first condition:
if (choice1 === "rock") {
if (choice2 === "scissors");
} else {
return ("rock wins");
}
So if choice1 is rock, you enter the if-block (which doesn't actually return anything, but since in this case choice1 is actually "paper" it goes into the else-block, which unconditionally returns "rock wins". Try refactoring it to be something like this:
if (choice1 === choice2) {
return("The result is a tie!");
}
if (choice1 === "rock") {
if (choice2 === "scissors") {
return ("rock wins");
} else {
return ("paper wins");
}
}
if (choice1 === "paper") {
if (choice2 === "rock") {
return ("paper wins");
} else {
return ("scissors wins");
}
}
if (choice1 === "paper") {
if (choice2 === "scissors") {
return ("scissors wins");
} else {
return ("rock wins");
}
}
But hey, let's get fancy. Try putting your choices into an array:
var choices = ["rock", "paper", "scissors"];
Now, notice that the item to the right always beats the item to the left (if we consider that the array wraps around). How can we use this to simplify the code? Well we can just compare the indexes of each choice, taking care to handle the edge case of scissors vs. rock:
var x = choices.indexOf(choice1),
y = choices.indexOf(choice2);
if (x === y) {
return("The result is a tie!");
} else if (x > y) {
if (x == 3 && y == 0) {
return choice2 + " wins";
} else {
return choice1 + " wins";
}
} else {
return choice2 + " wins";
}
But we can use the remainder operator (%) here to handle the edge case more easily:
var choices = ["rock", "paper", "scissors"];
var compare = function (choice1, choice2) {
var x = choices.indexOf(choice1),
y = choices.indexOf(choice2);
if (x === y) {
return("The result is a tie!");
}
return (((x - y) % 3) > 0 ? choice1 : choice2) + " wins";
}

Your function will always return "rock wins" when choice1 is not "rock". This is because you have used the if - else statement.
What you are doing is:
if choice1 is rock do something
else return "rock wins"
I will give you the first statement:
if (choice1 === "rock") {
if (choice2 === "scissors") return ("rock wins");
if (choice2 === "paper") return ("Paper wins");
}

jsFiddle Demo
There are interesting choices you made when using the if statements. There shouldn't be semi colons after them. Also, the logic composition can struggle when using many if then else statements. Often, it is best to use a switch case statementMDN in these situations.
var compare = function (choice1, choice2) {
if(choice1==choice2)return "The result is a tie!";
switch(choice1+choice2){
case "rockscissors": case "scissorsrock":
return "rock wins";
case "rockpaper": case "paperrock":
return "paper wins";
default: return "scissors wins";
}
};

if (choice1 === "rock") {
if (choice2 === "scissors");
} else {
return ("rock wins");
}
Look at this again. You say:
IF choice1 === rock, THEN
IF choice2 === scissors THEN DO NOTHING
ELSE (choice1 is not rock)
return 'rock wins'
This is a case where explicit braces would help. I'm guessing you meant to do this:
if (choice1 === "rock") {
if (choice2 === "scissors") {
}
} else {
return ("rock wins");
}

if the data is very controlled, you can just do:
if in (choice1+""+choice2) you find "ks", "rr" or "sp", you choice1
won, else lost (example below)
function getWinner(choice1, choice2){
var both_str, after_removing;
if(choice1 == choice2){
return "The result is a tie!";
}
both_str = (choice1 + "" + choice2);
after_removing = both_str.replace(RegExp("ks|rr|sp", "g"), "");
return (choice1 + ((both_str.length - after_removing.length) ? " won" : " lost"));
}
And you get the following results:
console.log(getWinner("scissors", "paper")); //scissors won
console.log(getWinner("rock", "scissors")); //rock won
console.log(getWinner("paper", "rock")); //paper won
console.log(getWinner("scissors", "rock")); //scissors lost
console.log(getWinner("rock", "paper")); //rock lost
console.log(getWinner("paper", "scissors")); //paper lost
console.log(getWinner("scissors", "scissors")); //The result is a tie!
console.log(getWinner("rock", "rock")); //The result is a tie!
console.log(getWinner("paper", "paper")); //The result is a tie!

Related

The prompt value returns undefined in Javascript [closed]

Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 6 years ago.
Improve this question
First of all, the user gets a prompt message that asks to choose one of the three options: rock, scissors or paper. The thing is after prompt message nothing happens.
I believe the value entered must be received before any of the comparisons start?
Is there something missing from the code so it cannot proceed to the next steps? The code I got from an online tutorial, but I believe the is not working from the beginning.
var userChoice = prompt("What would you like to play?"),
computerChoice = Math.random();
if (computerChoice <= 0.34){
computerChoice = "Rock";
}
else if (computerChoice >= 0.35 && computerChoice <= 0.67){
computerChoice = "Paper";
}
else {
computerChoice = "Scissors";
}
console.log("Computer's choice is " + computerChoice);
var compare = function(choice1, choice2) {
if (choice1 === choice2){
return "This 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";
}
}
};
console.log(compare(userChoice, computerChoice));
The code is correct and working properly, you just forgot the important part of showing it.
You aren't writing the result to the actual document. You call the function and it returns properly, but you aren't showing the answer visibly. Try this code:
var userChoice = prompt("What would you like to play?"),
computerChoice = Math.random();
if (computerChoice <= 0.34){
computerChoice = "Rock";
}
else if (computerChoice >= 0.35 && computerChoice <= 0.67){
computerChoice = "Paper";
}
else {
computerChoice = "Scissors";
}
document.write("Computer's choice is " + computerChoice);
var compare = function(choice1, choice2) {
if (choice1 === choice2){
return "This 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";
}
}
else {
return "Invalid input! Type either rock, paper, or scissors!"
}
};
document.write(", " + compare(userChoice, computerChoice));
You were missing the last document.write to write the result to the document. You can format it yourself. I'd also suggest adding another case if the input isn't valid. You could also put the main prompt in a function and call recursion if the input isn't valid.

Why is this javascript function call not working?

Calling my function yields a syntax error, everything else works on its own. The error is thrown on.
console.log(compare(userChoice, computerChoice));
The error is:
SyntaxError: Unexpected token {
Any ideas?
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";
}
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 "paper wins"
}
else
{
return "scissors wins"
}
}
}
console.log(compare(userChoice, computerChoice));
Yes, you forgot the ) at if (choice2 === "paper".
As others have pointed out, one of your closing )s was missing.
With lots of nested if-else blocks, it's easy to lose track of your ()s and {}s. You can write much shorter, nicer, easier to read code if you utilise other conditional statements and expressions, namely the switch statement and the ? operator:
function compare(choice1, choice2) {
if (choice1 === choice2) {
return 'The result is a tie';
}
switch(choice1) {
case 'rock':
return choice2 === 'scissors' ? 'rock wins' : 'paper wins';
case 'paper':
return choice2 === 'rock' ? 'paper wins' : 'scissors win';
case 'scissors':
return choice2 === 'paper' ? 'scissors win' : 'rock wins';
}
}
As you can see, this code has much fewer ()s and {}s.

Another Rock, Paper, Scissors

I'm just trying to build the first two steps of my r/p/s game, and here is my coding so far. It is giving me the response "Syntax error: unexpected token else" and I cannot for the life of me figure out why...
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";
}
}
You closed the main if/else chain with your paper wins section, I believe you meant to attach that else to the inner if/else as below.
In the paper section you had the same thing, I fixed that instance as well. From here you should be able to add the final if(choice1 === "scissors") segment.
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";
}
}
I fixed this for you:
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";
}
}
}
You've got a muddle of ifs and elses!
I believe specifically, you've got if (choice2 === "scissors") without curly braces, which is fine, except then you closed the brackets you never opened with an } else {. You should indent properly, it will help you see why you might have run into an issue.
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";
}
}
}
So you can see, I added { after any 'ifs' without, and then added two } to close those in the correct spots.
else {
return "Paper wins";
}
else if (choice1 === "paper") {
you cannot do a else if after doing a else;
I was negated by not giving the full answer and force him to study conditions?

Javascript game giving wrong output

I just completed a few exercises on "CodeAcademy" which taught me to create a rock,paper and scissors game. Everything works fine except the output that I want to receive. I am not getting the out "paper wins". All i get it "paper". I have just started to grasp the foundation of Javascript which is why I am not a strong scripter yet.
var userChoice = prompt("Do you choose rock, paper or scissors?");
var computerChoice = Math.random(0);
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!");
}
if (choice1 === "paper") {
if (choice2 === "rock") {
return "paper wins";
} else {
if (choice2 === "scissors") {
return "scissors wins";
}
}
if (choice1 === "scissors") {
if (choice2 === "rock") {
return "rock wins";
} else {
if (choice2 === "paper") {
return "scissors wins";
}
}
}
}
};
I think you are invoking the compare function on selection of the choices. so you are sending user choice to the function. now you should compare it with computerChoice. what is choice2 there?
As others have stated you never call your compare function and you never return any value of that function. And even if you did your function would fail most of the time anyway.
You structure:
if (choice1 === "paper") {
if (choice2 === "rock") {
return "paper wins";
} else {
if (choice2 === "scissors") {
return "scissors wins";
}
}
if (choice1 === "scissors") { //is never true because choice1 is always "paper"
if (choice2 === "rock") {
return "rock wins";
} else {
if (choice2 === "paper") {
return "scissors wins";
}
}
}
} //if choice1 === "paper" ends here
//no love for the rock?
What you want to achieve:
if (choice1 === "paper") {
if (choice2 === "rock")
{
return "paper wins";
}
else if (choice2 === "scissors")
{
return "scissors wins";
}
}
else if (choice1 === "scissors")
{
if (choice2 === "rock")
{
return "rock wins";
}
else if (choice2 === "paper")
{
return "scissors wins";
}
}
else if (choice1 === "rock") //don't forget the rock
{
if (choice2 === "paper")
{
return "paper wins";
}
else if (choice2 === "scissors")
{
return "rock wins";
}
}
Then you will have to call your function like this:
var result = compare(userChoice, computerChoice);
And you log the result variable. In your example you logged computerChoice which can only be "paper", "scissors" or "rock" because that is the value you give it, not the value of the function.
Since you let the user type in anything he/she wants you will obviously also have to validate their input or the if/else structure will fail.

I'm having trouble with the syntax for my compare function for javascript

I've included a screenshot image of a codeacademy error I made today. I'm trying to create a compare function that randomly picks a number between 0 and 1 (paper, scissors, or rock) that inputs two choices and returns the winner based on how choice1 compares to choice2.
The first part is a comment, but it explains how the original paper scissors rock function was built
Here is the code:
/*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";
}*/
var compare = function (choice1, choice2) {if (choice1 === choice2) return("The result is a tie!");
if (choice1 < 0.34)
if(choice2 ==="scissors");
return("rock wins");
} else if(choice2 ==="paper");{
return("paper wins");
};
};
It's telling me that there's an unexpected token else on line 15 (the else if line)
When I erase the else part it gives me another syntax error saying the same thing about a token if. I'm stuck on what part of my syntax is off and how to fix it.
I have a feeling it has something to do with === and the ; after the if() statements, either way, here is a nicer way of comparing them.
function compare(a,b)
{
if(a==b)return "draw";
switch(a)
{
case "rock":return (b=="scissors"?a:b)+" wins";
case "paper":return (b=="rock"?a:b)+" wins";
case "scissors":return (b=="paper"?a:b)+" wins";
}
}
console.log(compare("scissors","paper"));
function compare(choice1, choice2) {
if (choice1 === choice2) {
return "The result is a tie!";
}
if (choice1 < 0.34) {
if (choice2 === "scissors") {
return "rock wins";
} else if (choice2 === "paper") {
return "paper wins";
}
}
}
Check the comments on semicolon related errors below.
var compare = function (choice1, choice2) {
if (choice1 === choice2) return("The result is a tie!");
if (choice1 < 0.34) {
if(choice2 === "scissors") { // remove ; here
return("rock wins");
} else if (choice2 === "paper") { // remove ; here
return("paper wins");
} // remove ; here
} // add another else => what happens when choice1 >= 0.34 (not a rock)
};
With the required else blocks the complete function would look like:
var compare = function (choice1, choice2) {
if (choice1 === choice2) return("The result is a tie!");
if (choice1 < 0.34) { // rock
if(choice2 === "scissors") {
return("rock wins");
} else if (choice2 === "paper") {
return("paper wins");
}
} else if (choice <= 0.67) { // paper
if(choice2 === "rock") {
return("paper wins");
} else if (choice2 === "scissors") {
return("scissors wins");
}
} else { // scissors
if(choice2 === "paper") {
return("scissors wins");
} else if (choice2 === "rock") {
return("rock wins");
}
}
};
EDIT
This is just to help you overcome the confusion over semicolons if any. Usually a function definition is not required to have a ; after its body completes by putting the last closing curly brace }.
function compare (choice1, choice2) {
// ...
}
On the contrary, when we assign a variable a value, the statement is terminated by a semicolon.
var name = "John Doe";
Hence, when we combine the two, we're defining a function and then using it in an assignment statement which needs to be closed by using a semicolon. Hence, the syntax:
var compare = function (choice2, choice2) {
// ...
};

Categories

Resources