This forum is now read-only. Please use our new forums! Go to forums

0 points
Submitted by Ken M
over 10 years

[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

16 votes

Permalink

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.

points
Submitted by Judy
over 10 years

8 comments

Ken M over 10 years

Ah, that makes sense! Thanks for your clear and thorough explanation, AlbionsRefuge. I was wondering how the code checker does its magic.

Judy over 10 years

You’re welcome. Sometimes it does look for specific words in your code but not this time :)

surfer about 10 years

thanx AlbioRefuge,thanx Kenny M

paul.alamis almost 10 years

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”;
}

Judy almost 10 years

@paul.alamis, you are not allowed, and it isn’t logical, to put a condition (computerChoice > 0.67) on your else statement.

محمد الشعراوي almost 10 years

//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”; }

Judy almost 10 years

JavaScript has no “between” syntax such as you have used: 0 < computerChoice < 0.33

judgeAlexx over 9 years

you are a genius

Answer 52a0fec67c82ca56df000f5c

3 votes

Permalink

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”;

points
over 10 years

2 comments

Judy over 10 years

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.

Jordan Eloge about 9 years

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

0 votes

Permalink

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

points
Submitted by takiyuddin
over 9 years

2 comments

Pinlin Liu about 9 years

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

Judy about 9 years

@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

0 votes

Permalink

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.

points
Submitted by MirageOfGlory
over 9 years

2 comments

Reagan Williams about 9 years

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.

Judy about 9 years

You are correct Reagan.

Answer 547eaac49c4e9d3d4a000c14

0 votes

Permalink

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!

points
Submitted by kenneth
over 9 years

1 comments

Judy over 9 years

奥蒂诺 肯尼斯, 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

0 votes

Permalink

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”); }

points
Submitted by John.21
over 8 years

1 comments

Judy over 8 years

Hi John.21 are you asking for help or are you helping Kenny debug his code?