Lessons 123 to 125 – Complete 2020 Web Development Bootcamp

Lesson 123 – Control Statements: Using If-Else Conditionals and Logic

If-Else statements are conditionals that allow the code to do different things depending on the conditions that exist at the time the code is run. The following code contains if – else statements that enable the code to alert different messages to the user depending on the calculate percentage of the match between the two names input by the user.

var name = prompt("What is your name?");
var match = prompt("What is the name of your love match");

var n = Math.random();
n = Math.floor(n * 100) + 1;

if (n > 70) {
   alert(name + " and " + match + " are a " + n + "% match! You love each other like Kanye loves Kanye!");
} else {
    alert(name + " and " + match + " are a " + n + "% match!");
}

Lesson 124 – Comparators and Equality

The equal sign (“=”) and the greater and lesser signs (“>”, “<“) are examples of comparators.

Comparators allow us to compare two different values.

The following comparators carry out the checks set out next to each of them.

  • The (“===”) comparator checks whether the left hand side is equal to the right hand side.
  • The (“!==”) comparator checks whether the left hand side is not equal to the right hand side.
  • The (“>”) comparator checks whether the left hand side is greater than the right hand side.
  • The (“<“) comparator checks whether the left hand side is less than the right hand side.
  • The (“>=”) comparator checks whether the left hand side is greater than or equal to the left hand side.
  • The (“<=”) checks whether the left hand side is less than or equal to the right hand side.

The difference between the two equality comparators (“==”) and (“===”) is:

  • The comparator (“===”) checks for equality and for whether or not the data types are matching.
  • The comparator (“==”) only checks for equality. It does not check to see whether or not the date types are matching.

The result of this is that:

1 == "1";

will evaluate as “true” because the comparator (“==”) is only checking for equality but:

1 === "1"

will evaluate as false because the comparator (“===”) checks for both equality and the data types.

Lesson 125 – Combining Comparators

The three combiners that can be used are:

  • && – meanst AND
  • ||- means OR
  • ! – means NOT

This is the code from Lesson 117 adapted to incorporate the use of combiners to check whether two facts are true. The second “if” statement checks to see whether the variable “n” is greater than 30 and less than or equal to 70.

var name = prompt("What is your name?");
var match = prompt("What is the name of your love match");

var n = Math.random();
n = Math.floor(n * 100) + 1;

if (n > 70) {
   alert(name + " and " + match + " are a " + n + "% match! You love each other like Kanye loves Kanye!");
} 

if (n > 30 && n <= 70) {
    alert(name + " and " + match + " are a " + n + "% match!");
}

if (n <= 30) {
    alert(name + " and " + match + " are a " + n + "% match! You go together like oil and water.");
}

The challenge at the end of this lesson was to amended the BMI calculator code from this lesson so that it tells the user what their BMI is and whether or not they are underweight, normal weight or overweight.

This is my solution:

function bmiCalculator (weight, height) {
    var bmi = weight / (height * height);
    bmi = Math.round(bmi * 10) / 10;  
    
  if (bmi <= 18.5) {
    alert("Your BMI is " + bmi + " , so you are underweight.");
  }

  if (bmi > 18.5 && bmi <= 24.9) {
    alert("Your BMI is " + bmi + " , so you have a normal weight.");
  }

  if (bmi > 24.9) {
    alert("Your BMI is, " + bmi + " , so you are overweight.");
  }
}
bmiCalculator(63, 1.75);