DEV Community

Cover image for Scenario Based Questions-Conditional Statements(JS)
Kavitha
Kavitha

Posted on

Scenario Based Questions-Conditional Statements(JS)

1) Shopping Discount System

In online shopping websites, discounts are applied based on the purchase amount.

Rules

  • Amount ≥ 5000 → 20% discount
  • Amount ≥ 2000 → 10% discount
  • Otherwise → No discount
  • Members get an extra 10% discount

Program

<!DOCTYPE html>
<html>
<head>
    <title>Shopping Discount</title>
</head>
<body>
<script>
    let amount = 5000;
    let isMember = true;
    let discount = 0;

    if (amount >= 5000) {
        discount = 20;
    }
    else if (amount >= 2000) {
        discount = 10;
    }
    else {
        discount = 0;
    }

    if (isMember) {
        discount = discount + 10;
    }

    let finalPrice = amount - (amount * discount / 100);

    document.write("Final Price: " + finalPrice);
</script>
</body>
</html>
Enter fullscreen mode Exit fullscreen mode

Output

Final Price: 3500
Enter fullscreen mode Exit fullscreen mode

Explanation

  • Initial discount = 20%
  • Member discount = 10%
  • Total discount = 30%
  • Final price = 5000 - 1500 = 3500

2) Login Access Check

This is a basic login validation system.

Rules

Allow login only if:

  • Username = "admin"
  • Password = "1234"
  • User is not blocked

Program

<!DOCTYPE html>
<html>
<head>
    <title>Login Check</title>
</head>
<body>
<script>
    let username = "kavitha";
    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");
    }
</script>
</body>
</html>
Enter fullscreen mode Exit fullscreen mode

Output

Incorrect username
Enter fullscreen mode Exit fullscreen mode

Explanation

Since username is "kavitha" and not "admin", it prints:

Incorrect username
Enter fullscreen mode Exit fullscreen mode

3) Traffic Signal System

A simple real-time example using traffic signal colors.

Rules

  • Red → Stop
  • Yellow → Get Ready
  • Green → Go

Program

<!DOCTYPE html>
<html>
<head>
    <title>Traffic Signal</title>
</head>
<body>
<script>
    let color = "red";

    if (color == "red") {
        document.write("STOP!");
    }
    else if (color == "yellow") {
        document.write("GET READY!");
    }
    else {
        document.write("GO!");
    }
</script>
</body>
</html>
Enter fullscreen mode Exit fullscreen mode

Output

STOP!
Enter fullscreen mode Exit fullscreen mode

4) ATM Withdrawal System

This program checks whether the withdrawal is valid.

Rules

  • If amount > balance → Insufficient funds
  • If amount ≤ 0 → Invalid amount
  • Otherwise deduct amount

Program

<!DOCTYPE html>
<html>
<head>
    <title>ATM Withdrawal</title>
</head>
<body>
<script>
    let balance = 5000;
    let withdrawalAmount = 500;

    if (withdrawalAmount > balance) {
        document.write("Insufficient funds");
    }
    else if (withdrawalAmount <= 0) {
        document.write("Invalid amount");
    }
    else {
        let remaining = balance - withdrawalAmount;
        document.write("Remaining Balance: " + remaining);
    }
</script>
</body>
</html>
Enter fullscreen mode Exit fullscreen mode

Output

Remaining Balance: 4500
Enter fullscreen mode Exit fullscreen mode

5) Mobile Data Usage

This checks internet data usage.

Rules

  • Usage ≥ 100 → Limit exceeded
  • Usage ≥ 80 → Warning
  • Otherwise normal usage

Program

<!DOCTYPE html>
<html>
<head>
    <title>Data Usage</title>
</head>
<body>
<script>
    let usage = 90;

    if (usage >= 100) {
        document.write("Limit exceeded");
    }
    else if (usage >= 80) {
        document.write("Warning: nearing limit");
    }
    else {
        document.write("Usage normal");
    }
</script>
</body>
</html>
Enter fullscreen mode Exit fullscreen mode

Output

Warning: nearing limit
Enter fullscreen mode Exit fullscreen mode

6) Electricity Bill System

This calculates bill amount based on units consumed.

Rules

  • 0–100 → ₹5/unit
  • 101–300 → ₹7/unit
  • Above 300 → ₹10/unit

Program

<!DOCTYPE html>
<html>
<head>
    <title>Electricity Bill</title>
</head>
<body>
<script>
    let unit = 40;
    let bill;

    if (unit >= 0 && unit <= 100) {
        bill = 5 * unit;
    }
    else if (unit <= 300) {
        bill = 7 * unit;
    }
    else {
        bill = 10 * unit;
    }

    document.write("Electricity Bill Amount: " + bill);
</script>
</body>
</html>
Enter fullscreen mode Exit fullscreen mode

Output

Electricity Bill Amount: 200
Enter fullscreen mode Exit fullscreen mode

Top comments (0)