JS Exam 2: Conditions and Loops (10 mixed exercises)

JS Exam 2: Control Flow and Loops

Test your ability to make decisions and repeat actions using conditions and loops.

Exercise 1 If/Else Statements

Write code to check age:

  1. Define a variable `age` with a value.
  2. If age is greater than or equal to 18, print "Adult".
  3. Otherwise, print "Minor".
Solution
let age = 20;

if (age >= 18) {
    console.log("Adult");
} else {
    console.log("Minor");
}
Exercise 2 Else If Chains

Evaluate a student's score:

  1. Define a variable `score` (from 0 to 100).
  2. If the score >= 90 print "Excellent".
  3. If it is >= 75 print "Very good".
  4. If it is >= 50 print "Pass".
  5. Otherwise print "Fail".
Solution
let score = 85;

if (score >= 90) {
    console.log("Excellent");
} else if (score >= 75) {
    console.log("Very good");
} else if (score >= 50) {
    console.log("Pass");
} else {
    console.log("Fail");
}
Exercise 3 Switch Statement

Use `switch` to check the day:

  1. Define a variable `day` containing the day number (1-7).
  2. If it's 1 print "Sunday", 2 "Monday", and so on.
  3. Add a default case that prints "Invalid day".
Solution
let day = 3;

switch (day) {
    case 1:
        console.log("Sunday");
        break;
    case 2:
        console.log("Monday");
        break;
    case 3:
        console.log("Tuesday");
        break;
    default:
        console.log("Invalid day");
}
Exercise 4 For Loop

Use a `for` loop to print numbers:

  1. Print the numbers from 1 to 10.
  2. Print only the even numbers in that range.
Solution
for (let i = 1; i <= 10; i++) {
    if (i % 2 === 0) {
        console.log(i);
    }
}
Exercise 5 While Loop

Use a `while` loop for a countdown:

  1. Start from 5.
  2. Print the number and decrease it by 1 each iteration.
  3. Stop when the number reaches 0 and print "Lift off!".
Solution
let count = 5;

while (count > 0) {
    console.log(count);
    count--;
}

console.log("Lift off!");
Exercise 6 Looping Arrays

You have an array `fruits = ["Apple", "Banana", "Orange"]`:

  1. Use a `for` loop to print each element.
  2. Use a `for...of` loop to do the same in a modern way.
Solution
let fruits = ["Apple", "Banana", "Orange"];

// Traditional loop
for (let i = 0; i < fruits.length; i++) {
    console.log(fruits[i]);
}

// for...of
for (let fruit of fruits) {
    console.log(fruit);
}
Exercise 7 Loop Control (Break & Continue)

Inside a loop from 1 to 10:

  1. Skip printing the number 5 (continue).
  2. Stop the loop entirely when reaching 8 (break).
Solution
for (let i = 1; i <= 10; i++) {
    if (i === 5) {
        continue; // skip
    }
    if (i === 8) {
        break; // stop
    }
    console.log(i);
}
Exercise 8 Ternary Operator

Rewrite the following in one line:

  1. If `isMember` is true, set `fee` to 10.
  2. Otherwise set `fee` to 20.
Solution
let isMember = true;
let fee = isMember ? 10 : 20;

console.log(fee);
Exercise 9 Do...While Loop

Use `do...while`:

  1. Define a variable `i = 0`.
  2. Print the number and increment it by 1.
  3. Continue while `i` is less than 3 (note it runs at least once).
Solution
let i = 0;

do {
    console.log(i);
    i++;
} while (i < 3);
Exercise 10 Looping Object Properties (For...in)

You have an object `person = {name: "Ali", age: 30}`:

  1. Use a `for...in` loop to iterate over all properties.
  2. Print each key and value (for example: "name: Ali").
Solution
let person = { name: "Ali", age: 30 };

for (let key in person) {
    console.log(key + ": " + person[key]);
}
Smart Editor

Write code and see the result instantly

Try it free