5. Logical Operators


#1

I have no idea what I am doing, lol.

    var user = prompt("You find yourself waking up in the middle of Calculus class. Do you listen, run, or restroom?").toUpperCase();
    var random = (Math.floor(Math.random()) * 2)


    switch (user) {
        case 0 'LISTEN':
            if (case 0 && random===1){
                console.log("You end up spending your time in class and writing what is remaining of the notes.")
                }
            else{
                console.log("You sit there frustrated that you are wasting your time.")
                }
                break;
        case 1 'RUN':
            if (case 1 || random===1){
                console.log("The professor scratches your name off the participation list for this day. You also miss an answer to an extra credit question on the upcoming test!")
                }
            else{
                console.log("You walk out and the professor still doesn't really even gives a shit!")
                }
                break;
        case 2 'RESTROOM':
            if (case 2 && random===1){
                console.log("The professor does nothing to adversely affect your grade. However you miss an answer to an extra credit question on the upcoming test!")
                }
            else{
                console.log("The professor makes a joke of you while you are gone and you are shunned for the rest of your college career!")
                }
                break;
        default:
            console.log("What the heck is " + user + "? Idiot!")
    }


#2

@hackintoshftw1

In
https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Statements/switch
[quote]

switch (parameter-expression) {
  case value1:
    //Statements executed 
    //      when the VALUE of expression matches value1
    [break;]
  case value2:
     //Statements executed 
    //      when the VALUE of expression matches value2
    [break;]
 ...
  case valueN:
    //Statements executed 
    //      when the VALUE of expression matches valueN
    [break;]
  default:
    //Statements executed 
    //      when none of the VALUE's match the VALUE of the expression
    [break;]
}

= = = = = = = = = = = = = = = = = = = = = = = = = = = =

The switch statement evaluates an parameter-expression,
matching the parameter-expression's VALUE to a case clause,
and executes statements associated with that case.
( and with the break or return-statement will exit the switch statement )
[end-quote]

= = = = = = = = = = = = = = = = = = = = = = = = = = = =

In your case clause you are using an expression
An expression is any valid unit of code that resolves to a VALUE.

Conceptually, there are two types of expressions:
-1 those that assign a value to a variable
and
2- those that simply resolve to a VALUE.

So in your case clause you are using the 2nd kind of expression
and you do that by
using the variable user =comparing= it to the string Value 'LISTEN'
which will result in a Boolean VALUE

Thus your case clause VALUE 'LISTEN'
is matched against the VALUE of the parameter-expression

================================================

A Math.random()
will return a number Value between 0.000 =and= 0.9999 ( thus 1 is EXCLUDED )
https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Math/random

Also follow the link logical operators
https://developer.mozilla.org/en-US/docs/Web/JavaScript/Guide/Expressions_and_Operators


#3

@hackintoshftw1: Tananan... lol, I like your case study. But we use case so that we do not need to use if/else a lot.


#4

@mashuk,
From
http://stackoverflow.com/questions/2922948/javascript-switch-vs-if-else-if-else
read
Sometimes it's better to use neither. For example, in a "dispatch" situation, Javascript lets you do things in a completely different way:
and read
The performance difference between a switch and if...else if...else is small, they basically do the same work. One difference between them that may make a difference is that the expression to test is only evaluated once in a switch while it's evaluated for each if. If it's costly to evaluate the expression, doing it one time is of course faster than doing it a hundred times


#5

I did not think of the performance and I did not have any idea of "Dispatch Situation". But, of course it is good to know and thanks for letting me know. I will have a look.

I saw a bench mark test in Ruby section in Codecademy where they demonstrated why it is good to use symbol for performance. But that's a different topic but gave me a quick idea that we do programming for software performance too.I was thinking to learn more about performance. In the meantime, I will see your link. Thanks again.


#6

Thanks for the reply.

Actually I do know what I am doing, but IDK why it gives me a syntax error. It says,

SyntaxError: Unexpected string


#7

@hackintoshftw1,
You will have to change the CASE-clause

into
case 'LISTEN':


#8

Oh.

Thats. Strange! Since if you look at the "Hint" that they gave me, this is the example that they gave...

switch(month) {

    case 0:

        if(day == 1) {
            console.log("Happy New Year");
        }
        else {
            console.log("Happy January");
        }

        break;

    /* ... */
}

#9

Also, I removed the 0,1,2, etc. Still gives a syntax error...

SyntaxError: Unexpected token case


#10

@hackintoshftw1,
I the example as mentioned inyour prior post
the switch is comparing =month= with the number Value 0

So going back to your Original Post
where you used a CASE clause
case 0 'LISTEN':
This is NOT possible.....
you either use
case 0:
or
case 'LISTEN':

I hope you are aware that a prompt() statement will return a string Value
https://developer.mozilla.org/en-US/docs/Web/API/Window/prompt

Reference

google search
== the Book ==
javascript [your question] site:developer.mozilla.org

== discussions / opinions ==
javascript [your question] site:stackoverflow.com

== guidance ==
www.crockford.com
http://javascript.crockford.com/code.html <---JavaScript code-convention
http://javascript.crockford.com/survey.html
https://developer.mozilla.org/en-US/docs/Web/JavaScript/A_re-introduction_to_JavaScript
www.developer.mozilla.org/en-US/docs/Web/JavaScript/Guide
http://stackoverflow.com/questions/9549780/what-does-this-symbol-mean-in-javascript