JavaScript if...else practical excercises with solutions

Hey Everyone, We've gathered a set of practical JavaScript exercises focusing on if-else conditional statements, complete with solutions.

Check out these exercises to get a better handle on how to use conditional logic in your JavaScript programming.

Exercise 1

Write a JavaScript Program that takes a student's score as a variable and prints out the corresponding grade (A, B, C, D, or F).

Solution:

let score = 85;

if (score >= 90) {
    console.log("A");
} else if (score >= 80) {
    console.log("B");
} else if (score >= 70) {
    console.log("C");
} else if (score >= 60) {
    console.log("D");
} else {
    console.log("F");
}

// Output: B

Exercise 2

Write a JavaScript Program that determines whether a user's post is popular based on the number of likes. If a post has more than 100 likes, print "Popular," otherwise print "Not Popular."

Solution:

let likes = 120;

if (likes > 100) {
    console.log("Popular");
} else {
    console.log("Not Popular");
}

// Output: Popular

Exercise 3

3. Write a JavaScript Program that checks if a product is in stock based on its inventory quantity. If the quantity is greater than 0, print "In stock," otherwise print "Out of stock." (use ternay operator)

Solution:

let inventoryQuantity = 5;
inventoryQuantity > 0 ?  console.log("In stock") :  console.log("Out of stock")

// Output: In stock

Exercise 4

Write a JavaScript Program that applies a discount to a product based on the user's loyalty. If the user is loyal, apply a 10% discount; otherwise, apply a 5% discount.

Solution:

let isLoyalCustomer = true;
let originalPrice = 50;
let discount;

if (isLoyalCustomer) {
    discount = 0.1;
} else {
    discount = 0.05;
}

let discountedPrice = originalPrice - originalPrice * discount;
console.log("Discounted Price: $" + discountedPrice);

// Output: Discounted Price: $45

Exercise 5

Write a JavaScript Program that checks the status of a user's account. If the account is active, check if the user has completed their profile. If the profile is complete, print "Account is active and profile is complete"; otherwise, print "Account is active, but profile is incomplete."

Solution:

let isAccountActive = true;
let isProfileComplete = false;

if (isAccountActive) {
    if (isProfileComplete) {
    console.log("Account is active and profile is complete");
    } else {
    console.log("Account is active, but profile is incomplete");
    }
} else {
    console.log("Account is inactive");
}

// Output: Account is active, but profile is incomplete

Exercise 6

Create a JavaScript Program that classifies a person into age groups and genders. If the person is under 18, check their gender and print "Child" or "Teenager." If the person is 18 or older, check their gender and print "Adult Male" or "Adult Female."

Solution:

let age = 25;
let gender = "female";
let classification;

if (age < 18) {
    if (gender === "male") {
    classification = "Child (Boy)";
    } else {
    classification = "Child (Girl)";
    }
} else {
    if (gender === "male") {
    classification = "Adult Male";
    } else {
    classification = "Adult Female";
    }
}

console.log("Classification: " + classification);

// Output: Classification: Adult Female

Exercise 7

Create a JavaScript Program that simulates a bank account. Check the account balance, and if there are sufficient funds, allow a withdrawal. If the withdrawal amount is greater than the account balance, print an appropriate message.

Solution:

let accountBalance = 1000;
let withdrawalAmount = 500;

if (withdrawalAmount <= accountBalance) {
    accountBalance -= withdrawalAmount;
    console.log("Withdrawal successful. Remaining balance: $" + accountBalance);
} else {
    console.log("Insufficient funds. Cannot complete the withdrawal.");
}

// Output: Withdrawal successful. Remaining balance: $500

Exercise 8

Extend the previous JavaScript Program to include overdraft protection. If the withdrawal amount exceeds the account balance but is within the overdraft limit (set to $200), allow the withdrawal with a warning. If it exceeds the overdraft limit, deny the withdrawal.

Solution:

let accountBalance = 1000;
let overdraftLimit = 200;
let withdrawalAmount = 1200;

if (withdrawalAmount <= accountBalance + overdraftLimit) {
    if (withdrawalAmount <= accountBalance) {
    accountBalance -= withdrawalAmount;
    console.log("Withdrawal successful. Remaining balance: $" + accountBalance);
    } else {
    let overdraftUsed = withdrawalAmount - accountBalance;
    console.log("Withdrawal with overdraft. Overdraft used: $" + overdraftUsed);
    accountBalance = 0;
    }
} else {
    console.log("Withdrawal denied. Exceeds overdraft limit.");
}

// Output: Withdrawal with overdraft. Overdraft used: $200. Remaining balance: $0

Exercise 9

Write a JavaScript Program that determines whether a customer is eligible for a loan based on their credit score. If the credit score is above 700, check their income. If the income is sufficient, approve the loan; otherwise, deny the loan.

Solution:

let creditScore = 750;
let income = 30000;
let loanAmount = 5000;

if (creditScore > 700) {
    if (income >= loanAmount * 0.2) {
    console.log("Loan approved.");
    } else {
    console.log("Insufficient income. Loan denied.");
    }
} else {
    console.log("Low credit score. Loan denied.");
}

// Output: Loan approved.