DEV Community

mohandass
mohandass

Posted on

JavaScript scenario questions (variables,operators,conditional statements)

1. You are building a login system.Store username and password in variable and check whether are filled.

PROGRAM:

let name = "messi";
let password = 123456;
if(!name){
    console.log("please give user name")
}else if(!password){
    console.log("please give password")
} else{
    console.log("login successful")
}
Enter fullscreen mode Exit fullscreen mode

OUTPUT:
login successful

2. Create variable to store a product's name,price,and stock.Print message like:

"iPhone cost ₹60000 and only 5 left in stock"

PROGRAM:

let name = "iPhone 16";
let price = "₹60000";
let stock =  5 ;

console.log(`${name} costs ${price} and only ${stock} left in stock.`)

Enter fullscreen mode Exit fullscreen mode

OUTPUT:

iPhone 16 costs ₹60000 and only 5 left in stock.

3. Store a student's marks in 3 subjects and calculate total.

PROGRAM:

let marks = 70;
let marks1 = 90;
let marks2 = 50;
const total = marks + marks1+ marks2;
console.log("total:",total)

Enter fullscreen mode Exit fullscreen mode

OUTPUT:
total: 210

4. Swap two numbers without using a their variable.

PROGRAM:

let a = 10;
let b = 7;

a=a+b;
b=a-b;
a=a-b

console.log(a)
console.log(b)
Enter fullscreen mode Exit fullscreen mode

OUTPUT:
7 //a
10 //b

5. Create a variable that store whether a user is logged in or not and show a welcome message.

PROGRAM:

let isLoggedin = true;
if(isLoggedin){
    console.log("welcome!")
}else{
    console.log("please log in to continue")
}
Enter fullscreen mode Exit fullscreen mode

OUTPUT:
WELCOME!

6. An e-commerce website gives 10% discount.Calculate final price.-(TBD)

7. Check whether a number is even or odd using operator.

PROGRAM:

let i = 19;
if(i % 2 ==0){
    console.log("Even Number")
}else{
    console.log("Odd Number")
}

Enter fullscreen mode Exit fullscreen mode

OUTPUT:
Odd Number

8. Compare two passwords entered by user.

PROGRAM:

let password = "abc123"
let password1 = "abc123"
if(password == password1){
    console.log("Matched")
}else{
    console.log("Not Matched");
}
Enter fullscreen mode Exit fullscreen mode

OUTPUT:
Matched

9.A person is eligible to vote if age >=18.Write condition.

PROGRAM:

let personAge = 15;
if(personAge >= 18){
console.log("Is Eligible for Vote")
}else{
    console.log("Is Not Eligible for Vote")
}

Enter fullscreen mode Exit fullscreen mode

OUTPUT:
Is Not Eligible for Vote

10. Use logical AND to check:

  • User is logged in
  • User is admin Then allow dashboard access.

PROGRAM:

let isLoggedin = true;
let admin = true;
if(isLoggedin && admin){
    console.log("Allow dashboard access")
}else{
console.log(" Not allow dashboard access")
}

Enter fullscreen mode Exit fullscreen mode

OUTPUT:
Allow dashboard access

11. Create a function that calculate total cart amount.

PROGRAM:

const cart = [300,500,450,];

function amt(acc,val){
    return acc+val;
}
const totalamt= cart.reduce(amt)
console.log("Total :",totalamt)
Enter fullscreen mode Exit fullscreen mode

OUTPUT:
Total : 1250

12. Create a function to greet a user by name.

PROGRAM:

let name = "MESSI"
function greet(user){
    return greet(user)

}
console.log(`WELCOME! ${name} `)

Enter fullscreen mode Exit fullscreen mode

OUTPUT:
WELCOME! MESSI

13. Create a function to convert Celsius and Fahrenheit.

PROGRAM:

function fahrenheit(celsius){
    return (celsius*9/5) + 32 + "°F"
}

console.log(fahrenheit(10))
Enter fullscreen mode Exit fullscreen mode

OUTPUT:
50°F

14. Create a function that returns the largest of two numbers.

PROGRAM:

function findlargest(a,b){
    if(a > b){
        return a;
    }else{
        return b;
    }
}
const largnum = findlargest(10, 100);
console.log(largnum)

Enter fullscreen mode Exit fullscreen mode

OUTPUT:
100 //b

15. Create a function to calculate EMI for a loan.-(TBD)

16. If user role is "admin" show admin panel,else show user dashboard.

PROGRAM:

let admin = false;
if(admin == true){
console.log("Show admin panel")
}else{
    console.log("Show user dashbord")
}
Enter fullscreen mode Exit fullscreen mode

OUTPUT:
Show user dashbord

17. check if number is positive,negative, or zero.

PROGRAM:

function checknumber(num){
    if(num > 0){
        return "positive Number"
    }else if (num < 0){
return "negative Number"
    }else{
        return "Zero"
    }
}
const number = checknumber(11)
console.log(number)


Enter fullscreen mode Exit fullscreen mode

OUTPUT:
positive Number

18. Check if a year is leap year.

PROGRAM:

let year = 2004;
if(year % 4 == 0){
    console.log("leap year")
}else{
console.log("Not a leap year")
}

Enter fullscreen mode Exit fullscreen mode

OUTPUT:
leap year

19. Movie ticket pricing: Child(<12)-₹100 Adult-₹200.

PROGRAM:

let age = 15;

if(age <= 12){
    console.log("Movie ticket price ₹100")
}else{
    console.log("Movie ticket price ₹200")
}

Enter fullscreen mode Exit fullscreen mode

OUTPUT:
Movie ticket price ₹200

20. If marks >=90-Grade A 75-Grade B else-Fail.

PROGRAM:

let mark = 80;
if(mark >= 90){
    console.log("Grae A")
}else if(mark >= 75){
console.log("Grade B")
}else{
    console.log("Fail")
}


Enter fullscreen mode Exit fullscreen mode

OUTPUT:
Grade B

Top comments (0)