-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathDay_3.js
155 lines (119 loc) · 3.54 KB
/
Day_3.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
// Activity 1 : If-Else-Statements
// Task 1: Write a program to check if a number is positive, negative, or zero, and log the result to the console
let num = 10;
if (num > 0) {
console.log(`the number ${num} is a positive number`);
}
else if (num === 0) {
console.log(`the number ${num} is a zero`);
} else {
console.log(`the number ${num} is a negative number`);
}
// Output: the number 10 is a positive number
// Task 2: Write a program to check if a person is eligible to vote (age >= 18) and log the result to the console
const age = 18;
if(age>=18){
console.log("You are eligible for vote")
}
else {
console.log("You are not eligible for vote")
}
// Output: You are eligible for vote
// Activity 2: Nested If-Else statements
// Task 3: write a program to find the largest of three numbers using nested if-else statements
let a = 10;
let b = 20;
let c = 30;
if (a >= b) {
if (a >= c) {
console.log("a is the greatest");
} else {
console.log("c is the greatest");
}
} else {
if (b >= c) {
console.log("b is the greatest");
} else {
console.log("c is the greatest");
}
}
// Output => c is the greatest
// Activity 3: Switch Case
// Task 4: write a program that uses switch case to determine the day of the week based on a number (1-7) and log the day name to the console.
const key = 3;
switch (key) {
case 1:
console.log("Sunday")
break;
case 2:
console.log("Monday")
break;
case 3:
console.log("Tuesday")
break;
case 4:
console.log("Wednesday")
break;
case 5:
console.log("Thursday")
break;
case 6:
console.log("Friday")
break;
case 7:
console.log("Saturday")
break;
default:
console.log(`the provided key ${key} is invalid`)
break;
}
// Output => Tuesday
// Task 5: Write a program that uses a switch case to assign a grade ("A","B","C","D","F") based on a score and log the grade to the console
let studentMark = 10;
let grade;
switch (true) {
case (studentMark >= 90 && studentMark <=100):
grade = "A";
console.log(grade)
break;
case (studentMark >= 80 && studentMark <=90):
grade = "B";
console.log(grade)
break;
case (studentMark >= 70 && studentMark <=80):
grade = "C";
console.log(grade)
break;
case (studentMark >= 60 && studentMark <=70):
grade = "D";
console.log(grade)
break;
case (studentMark < 60):
grade = "F"
console.log(grade)
break;
default:
console.log("Your Marks are invalid")
}
// Output => F
// Activity 4: Conditional (Ternary) Operator
// Task 6: Write a program that uses the ternary operator to check if a number is even or odd and log the result to the console
const isOddOrEven = 3
const result = isOddOrEven % 2 == 0 ? "Even" : "Odd"
console.log(result) // Odd
// Activity 5: Combining Conditions
// Task 7: Write a program to check if a year is a leap year using multiple conditions (divisible by 4, but not 100 unless also divisible by 400) and log the result to the console
let year = 2024;
if (year % 4 === 0) {
if (year % 100 === 0) {
if (year % 400 === 0) {
console.log(`${year} is a leap year`);
} else {
console.log(`${year} is not a leap year`);
}
} else {
console.log(`${year} is a leap year`);
}
} else {
console.log(`${year} is not a leap year`); // 2024 is a leap year
}