JavaScript Topics
Day 9 :
Conditional statements:
1. if Statement
2. else Statement
3. else if Statement
4. Switch
Conditional statements:
1. if Statement and 2. else Statement:
var userAge = 10;
if (userAge >= 18) {
console.log("Welcome! You can open the website.");
} else {
console.log("Sorry, you are not old enough to open the website.");
}
// Ticket booking
var totalSeatsInTheater = 100;
var bookedSeatsInTheater = 70;
var availableSeatsInTheater = totalSeatsInTheater - bookedSeatsInTheater;
if (availableSeatsInTheater > 0) {
console.log("We have " + availableSeatsInTheater + " seats available.");
var numOfTicketsBooking = 2;
if (numOfTicketsBooking <= availableSeatsInTheater) {
console.log("Booking " + numOfTicketsBooking + " tickets...");
} else {
console.log("Sorry, there are not enough seats available.");
}
} else {
console.log("Sorry, the theater is fully booked.");
}
3. else if Statement:
var examScore = 45;
if (examScore >= 90) {
grade = "A";
} else if (examScore >= 80) {
grade = "B";
} else if (examScore >= 70) {
grade = "C";
} else if (examScore >= 50) {
grade = "D";
} else {
grade = "F";
}
console.log("Your grade is:" + grade);
4. Switch
var teaOrder = "jasmineTea";
switch (teaOrder) {
case "Chai":
message = "Chai ordered";
break;
case "jasmineTea":
message = "jasmineTea ordered";
break;
default:
message = "Not Available "
}
console.log(message);
Task 1: Check if a Number is Even or Odd
Write a JavaScript program that takes a number as input and displays whether it's even or odd.
Task 2: Determine the Greater Number
Write a JavaScript program that takes two numbers as input and displays which one is greater (or if they are equal).
Task 3: Check if a Number is Positive, Negative, or Zero
Write a JavaScript program that takes a number as input and displays whether it's positive, negative, or zero.
Interview Questions:
1. What is the difference between switch and if...else if...else when it comes to handling multiple conditions?
2. What is the purpose of the break statement in a switch statement?
3. How do you use the else if statement in JavaScript? Provide a code example.
No comments:
Post a Comment