DEV Community

Cover image for Scenario Based Questions-Conditional Statements in JS
Kesavarthini
Kesavarthini

Posted on

Scenario Based Questions-Conditional Statements in JS

1) Shopping Discount System

let amount = 6000;
let isMember = true;
let discount = 0;

if (amount >= 5000) {
    discount = 0.20;
} else if (amount >= 2000) {
    discount = 0.10;
}

if (isMember) {
    discount += 0.10;
}

let finalPrice = amount - (amount * discount);

console.log("Final Price:", finalPrice);
Enter fullscreen mode Exit fullscreen mode

2) Login Access Check

let username = "admin";
    let password = "1234";
    let isBlocked = false;

    if (username == "admin") {
        if (password == "1234") {
            if (!isBlocked) {
                console.log("User verified");
            }
            else {
                console.log("User blocked");
            }
        }
        else {
            console.log("Incorrect password");
        }
    }
    else {
        console.log("Incorrect username");
    }
Enter fullscreen mode Exit fullscreen mode

3) Traffic Signal System

let signal = "red";

switch (signal) {
    case "red":
        console.log("Stop");
        break;
    case "yellow":
        console.log("Get Ready");
        break;
    case "green":
        console.log("Go");
        break;
    default:
        console.log("Invalid signal");
}
Enter fullscreen mode Exit fullscreen mode

4) ATM Withdrawal

let balance = 5000;
let withdrawAmount = 2000;

if (withdrawAmount > balance) {
    console.log("Insufficient funds");
} else if (withdrawAmount <= 0) {
    console.log("Invalid amount");
} else {
    balance -= withdrawAmount;
    console.log("Remaining Balance:", balance);
}

Enter fullscreen mode Exit fullscreen mode

5) Mobile Data Usage

let usage = 90;

    if (usage >= 100) {
        console.log("Limit exceeded");
    }
    else if (usage >= 80) {
        console.log("Warning: nearing limit");
    }
    else {
        console.log("Usage normal");
    }
Enter fullscreen mode Exit fullscreen mode

Top comments (0)