How did you do?

  • -Did you get extra points?

Up to this point, we have learned about data types, JS native objects, and some of the methods that we have access to with native JS. But when do we get to do stuff?

Now! I have mentioned that you have to start thinking like a computer. In order to help us, we have logical operators!

  • Logical and ( && )
  • Logical or ( || )
  • Logical not ( ! )
  • I am !tooOld to enjoy Star Wars!

Truthy and Falsy

  • In logical operations, everything is evaluated to either truthy (a Boolean value for true) or falsy (a Boolean value for false.)

Great! Now we know when something is true or false. How can we compare values to begin making JavaScript work for us?

Enter the comparison operators!

  • Loose equal ( == )
  • Strict equal ( === )
  • Loose not equal ( != )
  • Strict not equal ( !== )
  • Quick note about objects: if you compare two objects that are assigned to two different vars, they will always be not equal.
  • Greater than ( > )
  • Less than ( < )
  • Less than or equal ( <= )
  • Greater than or equal ( >= )

In your day 3 in-class app.js file:

  1. Declare a variable and set it equal to any string.
  2. Declare a second variable and call new String() on the same string (this will create an instance of a JS string object.)
  3. Use the logical operators to determine if they are strictly equal.
  4. Now use the logical operators to determine if they are loosely equal.
  5. Why did you get the result you got?

Arithmetic Operators

  • Addition(+), Subtraction(-), Division(/), Multiplication(*)
  • Modulo(%)
  • Increment(++) and Decrement(--)
  • NaN
  • Note: parseInt() can be used to convert strings and floats to integers.
  • Assignment Operators
    • +=
    • -=
    • *=
    • /=

Below the previous code your app.js file:

  1. Prompt for a number value (provide a default of 10)
  2. Prompt for a second number value (provide a default of 10)
  3. Convert the prompted values into integers
  4. Add the numbers together and alert the user with the result

Control Flow is quite simple: it is how we control what pieces of our code get executed (run).

  1. if/else is the most basic version of control flow.
  2. Switch statements are another way of controlling flow.
  3. Ternaries are perfect for simple yes or no/ true or false controls. Just don't nest them!

In your app.js file:

  1. Asks if the user is over 18
  2. If the user is over 18, allow them to proceed, otherwise alert the user that they are not old enough.
  3. Now that all your users at this point are at least 18, ask if they are less than 80 years old.
  4. Now ask the users who are between 18 and 80 if they like Star Wars. If they are over 80, ask if they like prunes.
  5. If they say they don't Star Wars, end the program. In all other cases, alert them with a message.

In development (whether front or back end) you have to take edge cases into account. Basically, never trust your user to do what you would expect them to do. If you write a program that needs a string, make sure you check that they gave you a string and not a number or array. If it needs a number, check for that. We'll cover several examples of this in class.

On a new takehome-day3 branch, write a program that:

  1. Prompts your user for their name.
  2. Next, ask the user's favorite color. (Provide the user with the basic 10 colors as options i.e. red, orange, yellow, green, blue, pink, purple, black, white, and grey).
  3. Depending on the color, write 9 different responses for the possible answers. Include the user's name in the response message.