This forum is now read-only. Please use our new forums! Go to forums
Just wanted someone with more experienced to check
Hi I have past this bit of the training however my answer is always a tie and wondered if anyone could shed light on why it keeps happening would be much appreciated
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) {
compare = (choice1 === choice2);{
return "The result is a tie";
}
if (choice1 === "rock")
{
if (choice2 ==="scissors"){
return "rock wins";
}
else {
return "paper wins";
}
}
};
var compare = function(choice1, choice2) {
if (choice1 === choice2) {
return "The result is a tie."; {
if (choice1 === "rock") { // true so step in here
if (choice2 === "scissors") // false so skip rest
return "rock wins";
} else {
return "paper wins";
}
}
if (choice1 === "paper") { // false so skip to else
if (choice2 === "rock")
return "paper wins";
} else { // here is where you are getting returned from
return "scissors wins";
}
}
};
var compare = function (choice1, choice2) {
if (choice1 === choice2){
return "The result is a tie!"; {
if (choice1 === "scissors") {
if (choice2 === "rock")
return "rock wins";
} else {
return "scissors win";
}
}
}
};
compare (userChoice, computerChoice);
[Editted by Aden Fission: Fixed code formatting]
Answer 524bfd3babf821767800206b
- You have defined the function ‘compare’ thrice, which is unnecessary (and extremely likely to cause bugs). Get rid of the two excess function expressions. You can define all the necessary functionality within a single function.
Within the course, you are actually asked to make editions on the same function, not rewrite the function every time.
I’ll focus on (what appears to me) the most logically written function (the second one). Also note that commenting code is useful not only for other developers, but also for yourself (especially when you are just starting out): Here’s a model of the function
Use indentation liberally for yourself and curly brackets intelligently for your computer. Try to close a curly bracket as soon as you can (the code editor here automatically inserts curlies for you) and simply type your code within the curlies. Remember that anything not within a function’s curlies will NOT get executed when the function is called. Hopefully useful tut.
Answer 5250783bf10c60448f00a069
You can refer to this code,if you want.
snip
[Removed for providing uncommented/unexplained solution code. Please provide either hints or commented code to help in learning. Thanks! ~ Aden]
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
1 comments
Hey Ankit, nice to see your helpful attitude! :D Could you not give away the full answer next time, though? That really deters from the learning experience (and even if you do give away the full answer, comment it sufficiently to make it useful for learning). Thanks, and good luck!