This forum is now read-only. Please use our new forums! Go to forums
I need help
I cannot solve this one at all I don’t understand so I can’t solve this code
Answer 56011b629113cbd16e00004b
Sry but a little more information is needed could you post you’re code or explain the problem a little further? Also have a look at the FAQ thread maybe the problem is already covered here: https://www.codecademy.com/forum_questions/51fc4178f10c60f4eb001233
Answer 5606483451b8877f1400008a
الكود ده اشتغل معايا…this is the completed 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";
}
console.log("Computer: " + computerChoice);
var compare = function(choice1, choice2) {
if (choice1 === choice2) {
return "The result is a tie!"
} else if (choice1 === "scissors") {
if (choice2 === "rock") {
return "rock wins"
} else if (choice2 === "paper") {
return "scissors wins";
}
} else {
return "rock wins";
}
};
compare(userChoice, computerChoice);
Edit: formatting fixed by haxor789
Answer 560979a7e39efe6ca800026e
@haxor789 this is my code : : : : :
{
return "The result is a tie!"
} else if (choice1 === "scissors") {
if (choice2 === "rock") {
return "rock wins"
} else if (choice2 === "paper") {
return "scissors wins";
}
} else {
return "rock wins";
}
};
compare(userChoice, computerChoice);
Answer 5609ad2ae39efe0927000084
var compare = function(choice1, choice2) {
if (choice1 === choice2) {
return "The result is a tie!"
}
else if (choice1 === "scissors") {
if (choice2 === "rock") {
return "rock wins"
} else if (choice2 === "paper") {
return "scissors wins";
}
}
...
}
As said this part of the code is correct but why do you both try to go on with:
else {
return "rock wins";
}
this? This means if it is not a tie and choice1 is not scissors the result will always be “rock wins!”. What you need to do instead here is add 2 more cases dealing with “rock” and “paper” in the way you did for “scissors” e.g. add
else if (choice1 === "rock") {
if (choice2 === "scissors") {
return "rock wins"
} else if (choice2 === "paper") {
return "paper wins";
}
}
and one more for “paper”. Then you have all cases covered: choice1 being choice2 and choice1 being “rock”,”paper” or “scissors” with all the possible choices for choice2. As said there is an FAQ thread dealing with the game logic as well. Hope it gets a bit clearer.
Popular free courses
- Free Course
Learn SQL
In this SQL course, you'll learn how to manage large datasets and analyze real data using the standard data management language.Beginner friendly,4 LessonsLanguage Fluency - Free Course
Learn JavaScript
Learn how to use JavaScript — a powerful and flexible programming language for adding website interactivity.Beginner friendly,11 LessonsLanguage Fluency - Free Course
Learn HTML
Start at the beginning by learning HTML basics — an important foundation for building and editing web pages.Beginner friendly,6 LessonsLanguage Fluency
2 comments
it didn’t work
@Adam The general structure of the if and else if case is alright, just a bit confusing that you start with the scissors case when the exercises goes through it in rock, paper, scissors order but that is your choice. What is wrong here is that you added a default rock wins for any other cases. That might be suitable as a placeholder but it would be necessary to replace it with the other two cases :) @Shaimaa Hamdy again it would really help if you could tell what exercise you’re working on and what code you’ve already written.