Help


#1


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 === "rock")
{
if (choice2 === "scissors")
{
return "rock wins";
}
}
else {
return "paper wins";
} 

HELP I have no clue what is wrong


#2

your else if which check one of the three possible options for choice1, should be part of your compare function.

then inside your else if, you have an if and else to check for the possible choices of choice2. Your else is not nested inside else if


#3

how do I change that? Sorry I am really new.


#4

the curly brackets determines nesting:

var compare = function(choice1, choice2) 
{
    if(choice1 === choice2) 
{
    return "The result is a tie!";
} // closes if
}; // closes function

so the placing of the curly brackets is key here


#5

So what is wrong the return or else if the else if is what it is saying is wrong


#6

your else if should be in your function:

var compare = function(choice1, choice2) 
{
    if(choice1 === choice2) 
{
    return "The result is a tie!";
} // closes if
// else if should be here
// it should be part of your function
}; // closes function

#7

ok i did that and it fixed the else if but then come more errors popped up

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 === "rock")
};

{
if (choice2 === "scissors")
{
return "rock wins";
}
}
else
{
return "paper wins";
}
};

on the else after rock wins and the }; after the else if


#8

Are my explanations this poor? :frowning:

you are building a compare function to determine the winner, so this means inside your function you have a if statement to check if the result is a tie:

var compare = function(choice1, choice2) 
{
    if(choice1 === choice2) 
{
    return "The result is a tie!";
}
};

then after the if we are going to add the first of 3 else if statements to see what the user chose:

var compare = function(choice1, choice2) 
{
    if(choice1 === choice2) 
{
    return "The result is a tie!";
}
else if (choice1 === "rock")
}
};

inside the else if, we nest an if and else for possible options of what the computer picked.


#9

No they are not, I am just not getting it, im bout to give up on learning coding.


#10

your if and else should be inside your else if:

else if (choice1 === "rock"){
    // use if to check for possible choice of computer

   // use else for the only remaining option
}

#11

ok i got rock fixed what do i do bout the else before paper wins and thank you so much for helping me


#12

Now what is wrongvar compare = function(choice1, choice2)
{
if(choice1 === choice2)
{
return "The result is a tie!";
}
else if (choice1 === "rock")
{
}
};
if (choice2 === "scissors")
{
return "rock wins";
}

else

return "paper wins";


#13

else if (choice1 === "rock")
{
// if still not here?
// else is also missing
}

your if and else are still not nested correctly.

With every step you take, wonder why you take a certain step, and reflect how this step affect your program


#14

This topic was automatically closed 7 days after the last reply. New replies are no longer allowed.