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")
}
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.`)
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)
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)
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")
}
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")
}
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");
}
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")
}
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")
}
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)
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} `)
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))
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)
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")
}
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)
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")
}
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")
}
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")
}
OUTPUT:
Grade B
Top comments (0)