Whats wrong with my code? in lesson 5 of rock paper scissors


#1



Replace this line with your code. 

/*var choice1 = prompt("Do you choose rock, paper or scissors?");
var compare = function(choice1,choice2){
    if(choice1===choice2)
      return "The result is a tie";};
var choice2 = Math.random();
if (computerChoice < 0.34) {
	computerChoice = "rock";
} else if(computerChoice <= 0.67) {
	computerChoice = "paper";
} else {
	computerChoice = "scissors";
} console.log("Computer: " + computerChoice);*/


#2

can you give me your full code
@jay8978


#3

@rcodeman this is the full code ive written


#4

ok.you wrote

if (computerChoice < 0.34)
but where is your computerChoice value?

@jay8978


#5

OK THIS IS MY NEW CODE @rcodeman
/*var choice1 = prompt("Do you choose rock, paper or scissors?");
var compare = function(choice1,choice2){
if(choice1===choice2)
return "The result is a tie";};

var choice2 = Math.random();
if (choice2 < 0.34) {
choice2 = "rock";
} else if(computerChoice <= 0.67) {
Choice2 = "paper";
} else {
Choice2 = "scissors";
} console.log("Computer: " + choice2);*/


#6

else if(computerChoice <= 0.67)//wrong

i think you know why
@jay8978


#7

@rcodeman i corrected my code but it says "Did you create a function called compare?"
/*var choice1 = prompt("Do you choose rock, paper or scissors?");
var compare = function(choice1,choice2){
if(choice1===choice2)
return "The result is a tie";};

var choice2 = Math.random();
if (choice2 < 0.34) {
choice2 = "rock";
} else if(choice2 <= 0.67) {
choice2 = "paper";
} else {
choice2 = "scissors";
} console.log("Computer: " + choice2);*/


#8

you have already learned that the code inside a JavaScript function will execute when "something" invokes it.
The code in a function is not executed when the function is defined. It is executed when the function is invoked.
Some people use the term "call a function" instead of "invoke a function".
example:

function number(a, b) {
return a * b;
}
number(10, 2); //calling the function

so you have to call the function.

but your function is not good
if(choice1===choice2)

suposse choice1=rock
where choice2=?
you hav taken
choice2 = Math.random();
inside the function

think :bulb:
@jay8978


#9

Thankyou i was kind of irritated while doing this a and got lazy to look at whats happening, this is where i shouldnt give up!! Here the correct code @rcodeman
var compare=function(choice1,choice2){
if (choice1===choice2)
return "The result is a tie!";
};
compare("rock","rock")


#10

yes.you have to do somthing like that

compare("userChoice","computerChoice")//calling the function

userchoice will go to choice1 and computerChoice will go to choice2

@jay8978


#11

is it necessary to use braces for a single statement in if ?? @rcodeman


#12

yes :slight_smile:

if(condition)
{
statement;
}

@jay8978


#13

@jay8978 technically, not always. CTRL-F "Whenever we need to execute multiple statements inside a loop, we wrap them in curly braces" on Eloquent JavaScript and read the next two paragraphs.

However I would recommend it both for the reasons listed there and also because a lot of beginner errors can be found and resolved more easily simply by organizing the code well :slight_smile:


#14

i am learning JS too as a beginner but i just saw that problem somewhere...so take a look at the entire solution to compare with yours.
var userChoice = prompt("Do you choose rock, paper or scissors?");
var computerChoice = Math.random();
if (computerChoice < 0.33) {
computerChoice = "rock";
} else if(computerChoice <= 0.66) {
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 === "rock") {
if (choice2 === "scissors")
return "rock wins";
} else if (choice1 === "paper") {
if (choice2 === "rock")
return "paper wins";
} else if (choice1 === scissors) {
if (choice2 === "rock")
return "rock wins";
}
}
compare(userChoice, computerChoice);