Day 3: Control Structures
This is the day 3 of 30 Days Javascript challenge.
Screenshot :-
**
Activity 1: If-Else Statements**
Task 1: Check if a Number is Positive, Negative, or Zero
let number = 10;
if (number > 0) {
console.log("The number is positive."); // Output: The number is positive.
} else if (number < 0) {
console.log("The number is negative.");
} else {
console.log("The number is zero.");
}
This program checks whether a number is positive, negative, or zero and logs the appropriate message to the console.
Task 2: Check Voting Eligibility
let age = 20;
if (age >= 18) {
console.log("You are eligible to vote."); // Output: You are eligible to vote.
} else {
console.log("You are not eligible to vote.");
}
This program checks if a person is eligible to vote based on their age and logs the result.
Activity 2: Nested If-Else Statements
Task 3: Find the Largest of Three Numbers
let a = 5;
let b = 8;
let c = 3;
if (a >= b && a >= c) {
console.log("The largest number is " + a); // Output: The largest number is 8
} else if (b >= a && b >= c) {
console.log("The largest number is " + b);
} else {
console.log("The largest number is " + c);
}
This program uses nested if-else statements to find the largest of three numbers and logs the result.
Activity 3: Switch Case
Task 4: Determine the Day of the Week
let dayNumber = 3;
let dayName;
switch (dayNumber) {
case 1:
dayName = "Sunday";
break;
case 2:
dayName = "Monday";
break;
case 3:
dayName = "Tuesday";
break;
case 4:
dayName = "Wednesday";
break;
case 5:
dayName = "Thursday";
break;
case 6:
dayName = "Friday";
break;
case 7:
dayName = "Saturday";
break;
default:
dayName = "Invalid day number";
}
console.log(dayName); // Output: Tuesday
This program uses a switch case to determine the day of the week based on a number (1-7) and logs the day name to the console.
Task 5: Assign a Grade Based on Score
let score = 85;
let grade;
switch (true) {
case score >= 90:
grade = "A";
break;
case score >= 80:
grade = "B";
break;
case score >= 70:
grade = "C";
break;
case score >= 60:
grade = "D";
break;
default:
grade = "F";
}
console.log("Your grade is " + grade); // Output: Your grade is B
This program uses a switch case to assign a grade based on a score and logs the grade to the console.
Activity 4: Conditional (Ternary) Operator
Task 6: Check if a Number is Even or Odd
let num = 7;
let result = (num % 2 === 0) ? "Even" : "Odd";
console.log(result); // Output: Odd
This program uses the ternary operator to check if a number is even or odd and logs the result to the console.
Activity 5: Combining Conditions
Task 7: Check if a Year is a Leap Year
let year = 2024;
let isLeapYear;
if ((year % 4 === 0 && year % 100 !== 0) || year % 400 === 0) {
isLeapYear = true;
} else {
isLeapYear = false;
}
console.log(isLeapYear ? "Leap Year" : "Not a Leap Year"); // Output: Leap Year
This program checks if a year is a leap year using multiple conditions and logs the result to the console.
This guide will help you understand and implement various control structures in JavaScript. Happy coding!
Subscribe to my newsletter
Read articles from Alpit Kumar directly inside your inbox. Subscribe to the newsletter, and don't miss out.
Written by
Alpit Kumar
Alpit Kumar
I am a passionate web developer and open-source enthusiast on a captivating journey of coding wonders. With a year of experience in web development, my curiosity led me to the enchanting world of React, where I found a true calling. Embracing the magic of collaboration and knowledge-sharing, I ventured into the realm of open source, contributing to Digital Public Goods (DPGs) for the betterment of the digital universe. A firm believer in learning in public, I share my insights and discoveries through blogging, inspiring fellow coders to embark on their own magical coding odysseys. Join me on this thrilling adventure, where imagination and technology converge, and together, let's shape the future of the digital landscape! 🎩✨