4. Computer Choice: Part 2


#1


https://www.codecademy.com/courses/javascript-beginner-en-Bthev-mskY8/0/4?curriculum_id=506324b3a7dffd00020bf661#


In the pop-up box, it says, "Oops, try again. There was a problem with your syntax."
In the other box, it says, "SyntaxError: Unexpected token <="


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 && <= 0.66) {
computerChoice = "paper";
} else (computerChoice > 0.67) 
    computerChoice = "scissors";
}
console.log(computerChoice);


#2

@technut123,
The ELSE statement does NOT take a condition !!!!!

== the Book ==
https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Statements/if...else
https://developer.mozilla.org/en-US/docs/Web/JavaScript/Guide/Control_flow_and_error_handling


#3

@technut123,
In javascript
if you use the logical operator
javascript expects a Boolean Value of true or false on the =left= and =right= side.....

You could provide expressions like
computerChoice > 0.34 && computerChoice <= 0.66

== the Book ==
https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Operators/Logical_Operators
https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Operators/Operator_Precedence


#4

my build

var userChoice = prompt("Do you choose rock, paper or scissors?");
var computerChoice = Math.random();
    if (computerChoice < 0.34) {
        console.log( "rock" )
        }
        else if (computerChoice > 0.67) {
            console.log( "scissors" )
            }
            else {
                console.log( "paper")
                }
console.log(computerChoice)

#5

@tesingmylimit
You got a
+++++range division problem ++++++++++++++++++++++++++++++++

 0         0.33          0.34         0.67        0.9999
 | range1   |        lost-range        |  range2         |   range3   |
 |    IF    |           ???            |  ELSE-IF        |    ELSE    |
    rock                                    scissors          paper

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.


#6

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