If else and else?


#1

So I wanted to have a three way if else, so that the code would give me three possible outcomes. So far I understand the basic "if, else", which gives you two outcomes, but I need a third.

//So far I've tried this

var computerChoice = Math.random();

if (ComputerChoice <= 0.33) {
    
var ComputerMove =  console.log("The Computer chose rock!")}

else
{if (0.33 < ComputerChoice < 0.67) {
    
ComputerMove =  console.log("The Computer chose paper!")}
}
else
{ComputerMove = console.log("The Computer chose scissors!");
}

Theres always an error on the last else, and I can't figure out why! It'd be greatly appreciated if anyone can help!


#2

Hm.. well it would be better if you formatted your code like this, with proper indentations (it makes it easier to read).

if (condition1) {
    code code code;
} else if (condition2) {
    code code code;
} else {
    code code code;
}

Remember to add the semicolon (;) at the end of each line within the if / else if / else function, like above. Instead of using two if / else functions (which can be quite confusing), you can just use if / else if / else.

Notice the way you've written ComputerChoice versus the variable you declared.

And I've tested this line (0.33 < computerChoice < 0.67) but I do not think it works.


#3

/*************************************************************************************************************
It's with your else if statement and proper syntax, remember variables are case sensitive. It should look something like this. Hope this helps just learning myself =) You should be able to copy and paste this in to see how it works. Try not to use Capital letters to start a variable or function/method name, not that they can't be used but is just bad practice. They tend to be used for classes and objects which hold more precedence over functions and variables.
**************************************************************************************************************/

// creates a random # between 0 and 1 and store it in variable computerChoice
var computerChoice = Math.random();

// create a global variable that can be used inside and outside of the the if/else
var computerMove;

// If the random number of computerChoice is less than or equal to 0.33 change computerMove to rock
if (computerChoice <= 0.33) {
computerMove = "Rock";
}
// else if computerChoice is greater than 0.33 and less than 0.67 change computerMove to paper
else if(computerChoice > 0.33 && computerChoice < 0.67) {
computerMove = "Paper";
}
//else when computerChoice does not equal any of the above change computerMove to scissors
else {
computerMove = "Scissors";
}

// show the random number picked and the result of the number
console.log("The random number picked was: " + computerChoice);
console.log("The computer chose " + computerMove + "!");


#4

Following all of your advice I made my code look like this;

userChoice = prompt("Do you choose rock, paper or scissors?");

console.log("You chose " + userChoice + "!");

var computerChoice = Math.random();

{

console.log(computerChoice);

}

if (computerChoice <= 0.33)

{
    
var computerMove =  console.log("rock")}

else

{if

(computerChoice < 0.67) {
    
computerMove =  console.log("paper")}

else

{computerMove = console.log("scissors");

}

}

console.log("Whereas, the computer chose " + computerMove + "!");

Yet for some reason I keep on getting an undefined for my computerMove variable which means I'm doing something wrong aren't I?


#6

Yes , the errors being you don't need { } before and after console.log(computerChoice);

You should create the var computerMove before the IF, ELSE IF, ELSE loop. How you have it, it will only create the var computerMove once the first IF statement is ran. Meaning if it were to choose the ELSE IF or ELSE part of the loop before the computer rolling a # that is <= 0.33, it would not even know what computerMove was because it hasn't been declared yet.

Can't have computerMove = console.log.(WHATEVER); console.log is not a valid to use in declaring a value for a variable. Besides that you are missing a semi-colon after your statements inside the IF and the ELSE IF parts of your loop.

The ELSE IF part of the loop has the wrong syntax. Should be.... else if (comparison) {code}. The error made here also ends up giving you another extra set of {} brackets that are not needed.

if (comparison) {
code;
code;
}
else if (comparison) {
code;
code;
}
else {
code;
code;
}

Hope that helps a little bit.


#7

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