Rock paper scissors computerChoice


#1

How do you do if a variable is between two numbers?


#2

@fluffy11611,

+++++ 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.


#3

I'm not understanding why only ONE comparison is used for each of the three. For example, the first would be computerChoice <=0.33. I can't wrap my head around understanding that if the instructions tells us that computerChoice is in between 2 numbers.

I have a question about the &&. Where did they teach us that? I have also used your answer and it still won't work. Everytime it still just gives me scissors, even though the numbers are random. Below is my code

var userChoice = prompt("Do you choose rock, paper or scissors?")
var computerChoice = Math.random();
console.log(computerChoice)
if (0 <= computerChoice && computerChoice <= 0.33)
{
(computerChoice = "rock");
}

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

else (0.67 <= computerChoice && computerChoice < 1 )
{
(computerChoice = "scissors");
}


#4

@achen06
The ELSE statement does not take a condition

var computerChoice = Math.random();                                       - - - -
            V
           if (computerChoice < 0.33 ){
                computerChoice= "rock"; - - - > - - - -+ 
           }                                           |
           else if (computerChoice < 0.67){            V
                computerChoice= "paper"; - - - ->- - --+
           }                                           |
           else {                                      V
                computerChoice= "scissors"; -- -> - - -+
           }                                           |
                                                       |
                     + - - < - - - < - - - - -< - - - -+
                     |
                     V
console.log( computerChoice );

#5

i am getting
Oops, try again. Did you set var computerChoice = Math.random() ? The capital 'M' in 'Math' is important!


#6

@boardninja80140
Could you present the FULL code you are using ??


#7

Hi guys. what am I missing here?

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

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


#8

apparently you can't do multiple conditions in the if statement just yet
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);


#9

Your code above (your first set of code not the second post) can be improved by changing this way:

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

if(computerChoice>=0 && computerChoice <= 0.33)
 {
 computerChoice = "rock";
 }else if (computerChoice >=0.34 && computerChoice <= 0.66)
 {
 computerChoice = "paper";
 }
 else
 {
 computerChoice = "scissors";
 }
console.log(computerChoice);

Remember else is everything else you do not need to make it specific. Also have a look there is a difference between =< and <= . You say it or write it less than equal to. Not the other way. same for greater than equal to.


#10

@codesurfer81088,
The ELSE doesnot take a condition
@mashuk,
Visualizing
your IF ELSE-IF ELSE statement

+++++range division problem ++++++++++++++++++++++++++++++++
     0         0.33          0.34         0.66        0.9999
     | range1   | lost-range   |  range2    |   range3   |
     |    IF    |      ???     |  ELSE-IF   |    ELSE    |

(computerChoice >=0 && computerChoice =< 0.33)
(computerChoice >=0.34 && computerChoice =< 0.66)


#11

I agree it loses some range which get scissored :grinning: but that's not a problem that's why the else statement. Actually, codesurfer81088 else statement took a block instead of a statement. It will be a problem if the else statement take a block rather a statement. I might be wrong. What do you think?


#12

its all working finished this course a while ago thx


#15