-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathscript.js
More file actions
136 lines (92 loc) · 2.8 KB
/
script.js
File metadata and controls
136 lines (92 loc) · 2.8 KB
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
//Question 1
function calculateBMI(weight, height) {
const bmi = weight / (height * height);
if (bmi < 18.5) {
return `BMI: ${bmi.toFixed(1)} - Underweight`; //bmi.toFixed(1) rounds the number to one decimal place
} else if (bmi >= 18.5 && bmi <= 24.9) {
return `BMI: ${bmi.toFixed(1)} - Normal weight`;
} else if (bmi >= 25 && bmi <= 29.9) {
return `BMI: ${bmi.toFixed(1)} - Overweight`;
} else {
return `BMI: ${bmi.toFixed(1)} - Obese`;
}
}
// Using same height for testing purpose
console.log(calculateBMI(50, 1.7)); // Underweight
console.log(calculateBMI(65, 1.7)); // Normal weight
console.log(calculateBMI(80, 1.7)); // Overweight
console.log(calculateBMI(95, 1.7)); // Obese
// Question 2
function checkSeason(month) {
month = month.toLowerCase(); // Converts months to lower case
if (month === "september" || month === "october" || month === "november") {
return "Autumn";
}
else if (month === "december" || month === "january" || month === "february") {
return "Winter";
}
else if (month === "march" || month === "april" || month === "may") {
return "Spring";
}
else if (month === "june" || month === "july" || month === "august") {
return "Summer";
}
else {
return "Invalid month";
}
}
console.log(checkSeason("february")); // Winter
console.log(checkSeason("august")); // Summer
console.log(checkSeason("October")); // Autumn
console.log(checkSeason("May")); // Spring
// Question 3
// With Math.max
// Finding the bigger number
function findMax(a, b, c) {
return Math.max(a, b, c);
}
console.log(findMax(0, 10, 5)); // 10
console.log(findMax(0, -10, -2)); // 0
//Without Math.max
function findMax(a, b, c) {
let max = a;
if (b > max) {
max = b;
}
if (c > max) {
max = c;
}
return max;
}
console.log(findMax(0, 10, 5)); // 10
console.log(findMax(0, -10, -2)); // 0
// Question 4
function printArray(arr) {
for (let i = 0; i < arr.length; i++) {
console.log(arr[i]);
}
}
printArray([1, 2, 3, 4]);
// Question 5
function showDateTime() {
const now = new Date();
let day = now.getDate();
let month = now.getMonth() + 1; // months start from 0
let year = now.getFullYear(); // e.g 2026
let hours = now.getHours();
let minutes = now.getMinutes();
// add 0 in front if number is 1 digit
if (day < 10) day = "0" + day; // if day is 5 it becomes 05, 2 becomes 02 and so on
if (month < 10) month = "0" + month; // above comment in line 114 for line 115-117
if (hours < 10) hours = "0" + hours;
if (minutes < 10) minutes = "0" + minutes;
return `${day}/${month}/${year} ${hours}:${minutes}`;
}
console.log(showDateTime());
// Question 5
function printArray(arr) {
for (let i = 0; i < arr.length; i++) {
console.log(arr[i]);
}
}
printArray([1, 2, 3, 4]);