-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathday3.js
126 lines (91 loc) · 3.19 KB
/
day3.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
// 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 x = 7;
if(x>0){
console.log("number is positive");
} else if(x<0){
console.log("number is negative");
} else{
console.log("number is zero"); //Output:number is positive
}
// - Task 2: Write a program to check if a person is eligible to vote (age >= 18) and log the result to the console.
let age = 20;
if(age>18){
console.log("you are eligible to vote");
} else{
console.log("you are not eligible to vote");
} //Output:you are eligible to 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 num1 = 3, num2 = 5, num3 = 9;
let largeNum;
if(num1>num2){
if(num1>num3){
largeNum = num1;
}
} else if(num2>num3){
if(num2>num1){
largeNum = num2
}
} else{
largeNum = num3;
}
console.log(`largest Number is ${largeNum}`); //Output:largest Number is 9
// Activity 3: Switch Case
// - Task 4: Write a program that uses a switch case to determine the day of the week based on a number (1-7) and log the day name to the console.
let day = 3;
switch (day) {
case 1:
console.log("Monday");
break;
case 2:
console.log("Tuesday");
break;
case 3:
console.log("Wednesday");
break;
case 4:
console.log("Thursday");
break;
case 5:
console.log("Friday");
break;
case 6:
console.log("Saturday");
break;
case 7:
console.log("Sunday");
break;
} //Output:Wednesday
// - Task 5: Write a program that uses a switch case to determine a grade (A, B, C, D, F) based on a score and log the result to the console.
let score = 65;
switch (true) {
case score >= 85:
console.log("Grade = A");
break;
case score >= 75:
console.log("Grade = B");
break;
case score >= 65:
console.log("Grade = C");
break;
case score >= 40:
console.log("Grade = D");
break;
case score < 40:
console.log("Grade = F");
break; //Output:Grade = C
}
// 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.
let Number = 9;
let oddEven = (Number % 2 == 0) ? 'Even Number' : 'Odd Number';
console.log(oddEven); //Output:Odd Number
// 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 divisible by 100; or divisible by 400) and log the result to the console.
let year = 2024;
if ((year % 4 === 0 && year % 100 !== 0) || (year % 400 === 0)) {
console.log("It's a leap year");; // It's a leap year
} else {
console.log(" It's not a leap year");; // It's not a leap year
} //Output: It's a leap year