Skip to content

1.8 Conditional Statements -4-

Writing more conditions

Task 1: Read the following the code and check the result.

var mark = 92;
if(mark >= 90){
    var evaluation = "Excellent";
}else if(mark < 90 && mark >= 80 ){
    var evaluation = "Very good";
}else if(mark < 80 && mark >= 70){
    var evaluation = "Good";
}else{
    var evaluation = "Fail";
}
console.log(evaluation);

What is going here?

  1. JavaScript sets the value 92 to the variable mark.

  2. It checks whether 92 >= 90 is true or not. It is true. Then the evaluation will be "Excellent".

  3. In the console, you will see "Excellent".

Task 2: Read the following the code and check the result.

var mark = 85;
if(mark >= 90){
    var evaluation = "Excellent";
}else if(mark < 90 && mark >= 80 ){
    var evaluation = "Very good";
}else if(mark < 80 && mark >= 70){
    var evaluation = "Good";
}else{
    var evaluation = "Fail";
}
console.log(evaluation);

What is going here?

  1. JavaScript sets the value 85 to the variable mark.

  2. It checks whether 85 >= 90 is true or not. It is false.

  3. It checks whether 85 < 90 && 85 >= 80 is true or not. It is true, it sets the evaluation to Very Good.

  4. In the console, you will see "Very Good".

Task 3: Read the following the code and check the result.

var mark = 75;
if(mark >= 90){
    var evaluation = "Excelent";
}else if(mark < 90 && mark >= 80 ){
    var evaluation = "Very good";
}else if(mark < 80 && mark >= 70){
    var evaluation = "Good";
}else{
    var evaluation = "Fail";
}
console.log(evaluation);

What's going here?

  1. JavaScript sets the value 75 to the variable mark.

  2. It checks whether75 >= 90 is true or not. It is false.

  3. It checks whether 75 < 90 && 75 >= 80 is true or not. It is false.

  4. It checks whether 75 < 80 && 75 >= 70 is true or not. It is true. It sets the evaluation to "Good"

  5. In the console, you will see "Good".

Task 4: Read the following the code and check the result:

var mark = 60;
if(mark >= 90){
    var evaluation = "Excelent";
}else if(mark < 90 && mark >= 80 ){
    var evaluation = "Very good";
}else if(mark < 80 && mark >= 70){
    var evaluation = "Good";
}else{
    var evaluation = "Fail";
}
console.log(evaluation);

What is going here?

  1. JavaScript sets the value 60 to the variable mark.

  2. It checks whether 60 >= 90 is true or not. It is false.

  3. It checks whether 0 < 90 && 60 >= 80 is true or not. It is false.

  4. It checks whether 60 < 80 && 60 >= 70 is true or not. It is false.

  5. Then the else statement will be executed and the evaluation will be 'Fail'.

  6. In the console, you will see "Fail".

Note(1): We can test multiple conditions with if/else if/else statement.

Task 5: Test the room temperature:

  1. if the temperature is greater than or equal to 40, it is very hot.

  2. if the temperature is between 30 and 40 (30-inclusive), it is hot.

  3. if the temperature is between 25 and 30 (25-inclusive), it is okay.

  4. if the temperature is between 15 and 25 (15-inclusive), it is cool.

  5. otherwise, it is cold.

var temp = 30;
if(temp >= 40){
    var msg = "Very Hot";
}else if(temp < 40 && temp >= 30){
    var msg = "Hot";
}else if(temp < 30 && temp >= 25 ){
    var msg = "Okay";
}else if(temp < 25 && temp >= 15){
    var msg = "Cool";
}else{
    var msg = "Cold";
}
console.log(msg);

You will get: Hot

Task 6: Check the following code:

var value = 20;
if (value >= 10){
    if(value >=15){
        console.log("Pretty High")
    }else{
        console.log("Just High")
    }
}else{
    console.log("Low");
}

You will get: Pretty High

What is going on here?

  1. JavaScript engine sets the value of 20 to the variable value.

  2. It checks whether 20 >= 10 is true or not. It is true.

  3. It checks whether 20 >=15 is true or not. It is true.

  4. It displays "Pretty High" in the console.

Notice that: This is called nested if, with which you can write if statements inside each others.

Task 7: if the user is logged in, check whether he/she likes the post or not. If he/she likes the post, display "Yes, he liked the post." in the console, otherwise, display "He did NOT like the post.".

var isLoggedIn = true;
var likedPost = false;
if(isLoggedIn){
  if(likedPost){
    console.log("Yes, he liked the post.");
  }else{
    console.log("He did NOT like the post.")
  }
}

You will get: He did NOT like the post.