03 SERIES


What we need to learn: 

  • Prompt and alert
  • Control flow: else-if
  • Logical operators  + Comparision Operators 
  • Variables 

 

Prompt

Sometimes I have to get data from people, for example name or age, so I need to ask for this information through a popup window using the prompt method in the code. This information will be save in a variable

let myName = prompt (“What’s your name”);

let yourNumber = prompt (“Choose your number between 1-100”);

Alert

The alert command is used to print text on a new popup window. Consider the following example:

alert(“Hello!”);

 

Control Flow else – if

Let’s learn how we can ask JavaScript to think like us and make decisions the way we do.

We’ll start with human-speak. Many decisions we make everyday boil down to this sentence in some form:

“If something is true, let’s do option 1, or else, if it is false, let’s do option 2.”

This sentence looks fairly similar when we write it with JavaScript. See for yourself:

let needsCoffee = true;

if (needsCoffee === true) {

    console.log(‘Finding coffee’);

else {

    console.log(‘Keep on keeping on!’);

}

We can add more conditions to our if/else statement with else if. Check out how this fits into our current knowledge of if/else statements:

let stopLight = ‘green’;

if (stopLight === ‘red’) {

  console.log(‘Stop’);

else if (stopLight === ‘yellow’) {

  console.log(‘Slow down’);

else if (stopLight === ‘green’) {

  console.log(‘Go!’);

else {

  console.log(‘Caution, unknown!’);

}

 

Logical and compararision operators

 

JavaScript supports the following math operators:

Add: +

Subtract: –

Multiply: *

Divide: /

let x = 4;
x =x+2; // x equals 6

let x = 4;
x += 2; // x equals 6

let y = 4;
y -= 2; // y equals 2

let z = 4;
z *= 2; // z equals 8

let r = 4;
r++; // r equals 5

let t = 4;
t–; // t equals 3

There are two comparisons you might be familiar with:

      • Less than: <
      • Greater than: >

You may also recognize these:

      • Less than or equal to: <=
      • Greater than or equal to: >=

These comparisons evaluate to true or false.

      • To check if two things equal each other, we write === (three = signs in a row).
      • To check if two things do not equal each other, we write !== (an exclamation with two = signs in a row).

It can be confusing when to use one = sign and when to use three === signs. Use a single = to assign a value to a variable. Use ===to compare the values of two different variables.

We can translate certain thoughts into JavaScript code such as, “Are these things equal?” with ===, or, “Is one thing greater than another thing?” with >.

In English, sometimes we say “both of these things” or “either one of these things.” Let’s translate those phrases into JavaScript with special operators called logical operators.

  • To say “both must be true,” we use &&.
  • To say “either can be true,” we use ||.

For example:

if (stopLight === ‘green’ && pedestrians === false) {

  console.log(‘Go!’);

else {

  console.log(‘Stop’);

}

  • In the example above, we make sure that the stopLight is ‘green’ and (&&) there are no pedestrians before we log Go!.
  • If either of those conditions is false, we log Stop.

Just like the operators we learned previously, these logical operators will return either true or false.

These logical operators are helpful when writing if/else statements since they let us make sure multiple variables are true or false. We can combine these operators with all of the ones we have learned throughout this lesson.

 

Sé el primero en comentar

Deja una respuesta