Lesson 4


#1

My code looks like this:

var userChoice=prompt("Do you choose rock, paper or scissors?")
var computerChoice=Math.random();

console.log(computerChoice)

if (computerChoice <= 0.33){
computerChoice="rock";
}
else if (computerChoice >=0.34) (computerChoice <= 0.66){
computerChoice="paper";
}
else (computerChoice >=0.67) (computerChoice < 1){
computerChoice="scissors";
}

And it says SyntaxError: Unexpected token {

Did i put bracket(s) in the wrong place?


#2

@filip666016,

An ELSE-statement does not take a CONDITION
change your

else (computerChoice >=0.67) (computerChoice < 1){

into

else {

#3

Hi here

else if (computerChoice >=0.34) (computerChoice <= 0.66){
computerChoice="paper";
}
else (computerChoice >=0.67) (computerChoice < 1){
computerChoice="scissors";
}

Your else if statement should only have one condition and your else statement shouldn't have a condition
So your else if should be like that

else if (computerChoice <= 0.66){
computerChoice="paper";
}

And your else statement

else {
computerChoice="scissors";
}

#4

@filip666016,

+++++ if else-if else skeleton ++++++++++
The Math.random() method will deliver a number with a value
in the range of 0_(included)_ upontill 0.9999_(1 excluded)_.

As you will have to divide the number range form 0_(inclusive)_ to 0,9999_(so 1 excluded)_
into 3 equal partitions
we have to use an if else-if else statement.
The if else-if else skeleton looks like:

    if (conditionA) {
               //conditionA is true
                //your code 
     } else if (conditionB) {
               //conditionA is false
               //conditionB is true
              //your code 
    } else {     // <=== !! Takes NO condition-statements !!
               //conditionA is false
               //conditionB is false
              //your code 
    }

following the Instructions the conditions would be:

  1. If computerChoice is between 0 and 0.33, make computerChoice equal to "rock".
    ( 0 <= computerChoice && computerChoice <= 0.33) which we capture at the IF using
    ( computerChoice <= 0.33)

  2. If computerChoice is between 0.34 and 0.66, make computerChoice equal to "paper".
    ( 0.33 < computerChoice && computerChoice < 0.67) which we capture at the ELSE IF as
    ( computerChoice < 0.67 )

  3. If computerChoice is between 0.67 and 1, make computerChoice equal to "scissors".
    ( 0.67 <= computerChoice && computerChoice < 1) , you reached the ELSE level
    you can asume that computerChoice is greater equal to 0.67,
    the ELSE does NOT take a condition, just write your code.


#5

Thanks, i will remember this for the future!


#6

Thanks for VERY detailed answer


#7

What is the meaning of the '&'?


#8

@arraysolver20132,

From the operators
https://developer.mozilla.org/en-US/docs/Web/JavaScript/Guide/Expressions_and_Operators
choose the logical operators
https://developer.mozilla.org/en-US/docs/Web/JavaScript/Guide/Expressions_and_Operators#Logical_operators


#9

Thank you for this.

I understand the issue I was having now. The Else is literally an "everything else" so you don't need any condition. Because you know if A (if) and B (else if) is false then C (else) will be true. I hope I'm understanding this correctly.


#10

Thank you, I was going through with same trouble.