DEV Community

Nanthini Ammu
Nanthini Ammu

Posted on

if else scenarios 1

    //  1)Shopping Discount System
    // You have:
    // amount
    // isMember(boolean)  
    // A user buys products worth amount.
    // if amount ≥ 5000 => 20% discount
    // if amount ≥ 2000 => 10% discount
    // otherwise => no discount
    // members get extra 10% discount
    // Print final price after discount.

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

        if(amount>=5000){
            discount=isMember ? 30 : 20;
        }else if(amount>=2000){
            discount=isMember ? 20 : 10;
        }else{
            discount= 0;
        }
        let finalAmount = amount - (amount * discount / 100);
        console.log(`Final amount to be paid: ${finalAmount} after applying a discount of ${discount}%`);
Enter fullscreen mode Exit fullscreen mode
//  2)Login Access Check
//     you have:
//         username
//         password
//         isBlocked(boolean)
//     allow login only if:
//         username is "admin"
//         password is "1234"
//         user is not blocked
//     otherwise print appropriate message.

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

if(username==="admin" && password==="1234" && !isBlocked){
    console.log("Login successful");
}
else if(username==="admin" && password==="1234" && !isBlocked){
    console.log("User is blocked");
}
else if(username!=="admin"){
    console.log("Incorrect username");
}
else if(username==="admin" && password!=="1234"){
    console.log("Incorrect password");
}
Enter fullscreen mode Exit fullscreen mode
// 3)Traffic Signal System
//     Signal color variable:
//         red = stop
//         yellow = get ready
//         green = go
//     print action based on color.

let signalColor = "Red";
if(signalColor.toLowerCase() === "Red".toLowerCase()){
    console.log("Stop");
}
else if(signalColor.toLowerCase() === "Yellow".toLowerCase()){
    console.log("Get ready");   
}
else if(signalColor.toLowerCase() === "Green".toLowerCase()){
    console.log("Go");
}    
else{
    console.log("Invalid signal color");   
 }
Enter fullscreen mode Exit fullscreen mode
// 4)ATM Withdrawal
//     User has:
//         balance
//         withdrawAmount
//     Rules:
//         If withdrawAmount > balance = "Insufficient funds"
//         If withdrawAmount ≤ 0 = "Invalid amount"
//         Else => deduct and show remaining balance



let balance = 5000;
let Withdrawal = 2000;

if(Withdrawal>balance){
    console.log("Insufficient funds" )
}else if(Withdrawal<=0)
    console.log("Invalid amount")
else{
    console.log(`Amount after Withdrawal: ${balance-Withdrawal}`)
}
Enter fullscreen mode Exit fullscreen mode
// 5)Mobile Data Usage
//     User has used data GB out of limit GB.
//     If usage ≥ 100% = "Limit exceeded"
//     If usage ≥ 80% => "Warning: nearing limit"
//     Else = "Usage normal"

    let dataUsed = 80; // in GB

    if (dataUsed >= 100) {
        console.log("Limit exceeded");
    } else if (dataUsed >= 80) {
        console.log("Warning: nearing limit");
    } else {
        console.log("Usage normal");
    }
Enter fullscreen mode Exit fullscreen mode
        // 6)Electricity Bill
        //     Units consumed:
        //         0-100 => 5/unit
        //         101-300 => 7/unit
        //         300+  10/unit
        //     Calculate total bill.



        let unitsConsumed = 90;
        let unitPrice = 0;

        if (unitsConsumed > 0 && unitsConsumed <= 100) {
            unitPrice = 5;

        } else if (unitsConsumed >= 101 && unitsConsumed <= 300) {
            unitPrice = 7;

        } else if (unitsConsumed >= 301) {
            unitPrice = 10;
        }

        console.log(`Total Bill Value is ${unitsConsumed * unitPrice}`);
Enter fullscreen mode Exit fullscreen mode

Top comments (0)