This forum is now read-only. Please use our new forums! Go to forums
[resolved] Code validates but isn't right, how can I debug it?
Code validates even though I forgot to put if(choice1 === “rock”) condition?
The following code passes, even though I forgot to include the if(choice1 === “rock”) condition. Possible error in the validator? I only realized it during the next lesson, when I was asked to write the next condition.
/*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 (choice2 === "scissors") {
return "rock wins";
} else {
return "paper wins";
}
};
Code formatted by Jacob Andersen
Answer 521d3989f10c60358400064c
That is an interesting case Kenny. The code checker is looking for results by calling your function and checking the results not by looking at the exact code. The reason for that is because there are many ways to go about solving this so there will be many different versions written by all the learners.
If you call it with “rock” and anything you will get the correct results.
console.log(compare(“rock”, “rock”)); // will get you “The result is a tie!” console.log(compare(“rock”, “paper”)); // will get you “paper wins” console.log(compare(“rock”, “scissors”)); // will get you “rock wins”
You are correct that this bug will not become apparent until the next step.
Answer 52a0fec67c82ca56df000f5c
Can anyone explain me this part?
var compare = function(choice1,choice2) { if (choice1 === choice2) { return “The result is a tie!”; } if (choice2 === “scissors”) { return “rock wins”; } else { return “paper wins”; } };
Where does the parameters “choice1 and choice2 came from? How are they refering to computerChoice and userChoice?
and this part (I’m very confused)
if (choice2 === “scissors”) { return “rock wins”;
2 comments
Functions just sit there waiting to be called, when you call your function you will call it with userChoice and computerChoice - then the function will turn those into choice1 and choice2. You are right to be confused about the choice2 === “scissors” :) I’m not sure what you mean to accomplish with that.
choice 2 references the computers choice, and the code for the computers choice has already been written. The computer generates a random number between 0 and 1, and based on that picks rock, paper, or scissors. in this case, if choice 2, the computers choice, is equal to scissors, the code will run.
Answer 546531fb9c4e9d0c400001df
var computerChoice1 = Math.random() if (computerChoice1 < 0.3) { computerChoice1 = “rock”; } else if(computerChoice1 <= 0.6) { computerChoice1 = “paper”; } else { computerChoice1 = “scissors”; }
console.log(computerChoice1);
var userChoice = prompt(“Which one do you use?”);
var compare = function(userChoice) { if (userChoice == computerChoice1) console.log(“The result is a tie!”);
else if(UserChoice == “rock”)
{
if(computerChoice1 == “paper”)
console.log(“paper wins”);
else if(computerChoice1 == "scissors")
console.log("rocks it");
} else if(UserChoice == “paper”) { if(computerChoice1 == “rock”) console.log(“paper wins”); else if(computerChoice1 == “scissors”) console.log(“cut it”); } else if(UserChoice == “scissors”) { if{(computerChoice1 == “paper”) console.log(“scissors”);
else if(computerChoice1 == "rock")
console.log("rocks it");
}
} this the code bu i cant get result. Can you help me please
2 comments
var userChoice = prompt( “Do you choose rock, paper or scissors?”); var computerChoice = Math.random(); console.log(computerChoice) ;
if ( computerChoice=0,0.33) { computerChoice=”rock”;}
else if ( computerChoice<0.66 ) { computerChoice=”paper”;}
else ( computerChoice=”scissors”);
@Pinlin, since this thread is about “how to debug”, let’s try that. Here is your code: http://www.codecademy.com/AlbionsRefuge/codebits/5Anhbl/edit – try different values of computerChoice and see if you think there is a bug in your code.
Answer 5475256b80ff338154000ee7
var userChoice = prompt("Do you choose rock, paper or scissors?");
var computerChoice = Math.random();
console.log(computerChoice);
if (computerChoice > 0) {
computerChoice = "rock";
}else if (computerChoice <= .33){
computerChoice = "rock";
}else if (computerChoice > .34){
computerChoice = "paper";
}else if (computerChoice <= .66){
computerChoice = "paper";
}else if (computerChoice > .67){
computerChoice = "scissors";
}else if (computerChoice <= 1){
computerChoice = "scissors";
}
This way works (albeit messy) and seems easier to understand to me.
2 comments
Does this work? It seems like you’d get rock every time because the first If statement comes back as true because every number is greater than zero.
You are correct Reagan.
Answer 547eaac49c4e9d3d4a000c14
this worked for me, i guess everything was correct;
var userChoice = prompt("Do you choose rock, paper or scissors?"); var computerChoice = Math.random(); console.log(computerChoice); if (0 <= computerChoice >= 0.33) { computerChoice = "rock"; } else if (0.34 <= computerChoice <= 0.66) { computerChoice = "paper"; } else if (0.67 <= computerChoice <= 1) { computerChoice = "scissors"; }
See which of these is user-friendly to you!
1 comments
奥蒂诺 肯尼斯, Since the topic of this thread is “how to debug” your code, you should try that. User friendly isn’t really very friendly if the results aren’t correct. The best way to know if you code is working it to print out the results. Run some sample values of computerChoice through your if statement and see what you get as a result. You can use Codebits for that, make one yourself or try mine: http://www.codecademy.com/AlbionsRefuge/codebits/GC5QdD/edit
Answer 55e45deae39efe955e00033f
var userChoice = prompt(“Do you choose rock, paper or scissors?”); var computerChoice = Math.random(); console.log(“User: “+userChoice); if(computerChoice <= 0.33 && computerChoice >= 0){ console.log(“Computer: “+”Rock”); }else if(computerChoice <=0.66 && computerChoice >=0.34){ console.log(“Computer: “+”paper”); }else if(computerChoice <=1 && computerChoice >=0.67){ console.log(“Computer: “+”scissors”); }
1 comments
Hi John.21 are you asking for help or are you helping Kenny debug his code?
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
8 comments
Ah, that makes sense! Thanks for your clear and thorough explanation, AlbionsRefuge. I was wondering how the code checker does its magic.
You’re welcome. Sometimes it does look for specific words in your code but not this time :)
thanx AlbioRefuge,thanx Kenny M
var userChoice = prompt(“Do you choose rock, paper or scissors?”); var computerChoice = Math.random(userChoice);
console.log(computerChoice);
if (computerChoice > 0.33) { computerChoice === “rock”; } else if((computerChoice > 0.33) && (computerChoice <= 0.66)) { computerChoice === “paper”; } else(computerChoice > 0.67) { computerChoice === “scissor”;
}
@paul.alamis, you are not allowed, and it isn’t logical, to put a condition (computerChoice > 0.67) on your else statement.
//look to this var userChoice = prompt (“Do you choose rock, paper or scissors?”) var computerChoice = Math.random(); console.log (computerChoice); if ( 0 < computerChoice < 0.33 ) { computerChoice = “rock”; } else if (0.34 < computerChoice < 0.66) { computerChoice = “paper”; } else { computerChoice = “scissors”; }
JavaScript has no “between” syntax such as you have used: 0 < computerChoice < 0.33
you are a genius