|
| 1 | +//Question 1 |
| 2 | + |
| 3 | +function calculateBMI(weight, height) { |
| 4 | + const bmi = weight / (height * height); |
| 5 | + |
| 6 | + if (bmi < 18.5) { |
| 7 | + return `BMI: ${bmi.toFixed(1)} - Underweight`; //bmi.toFixed(1) rounds the number to one decimal place |
| 8 | + } else if (bmi >= 18.5 && bmi <= 24.9) { |
| 9 | + return `BMI: ${bmi.toFixed(1)} - Normal weight`; |
| 10 | + } else if (bmi >= 25 && bmi <= 29.9) { |
| 11 | + return `BMI: ${bmi.toFixed(1)} - Overweight`; |
| 12 | + } else { |
| 13 | + return `BMI: ${bmi.toFixed(1)} - Obese`; |
| 14 | + } |
| 15 | +} |
| 16 | + |
| 17 | + |
| 18 | +// Using same height for testing purpose |
| 19 | +console.log(calculateBMI(50, 1.7)); // Underweight |
| 20 | +console.log(calculateBMI(65, 1.7)); // Normal weight |
| 21 | +console.log(calculateBMI(80, 1.7)); // Overweight |
| 22 | +console.log(calculateBMI(95, 1.7)); // Obese |
| 23 | + |
| 24 | + |
| 25 | + |
| 26 | +// Question 2 |
| 27 | + |
| 28 | +function checkSeason(month) { |
| 29 | + month = month.toLowerCase(); // Converts months to lower case |
| 30 | + |
| 31 | + if (month === "september" || month === "october" || month === "november") { |
| 32 | + return "Autumn"; |
| 33 | + } |
| 34 | + else if (month === "december" || month === "january" || month === "february") { |
| 35 | + return "Winter"; |
| 36 | + } |
| 37 | + else if (month === "march" || month === "april" || month === "may") { |
| 38 | + return "Spring"; |
| 39 | + } |
| 40 | + else if (month === "june" || month === "july" || month === "august") { |
| 41 | + return "Summer"; |
| 42 | + } |
| 43 | + else { |
| 44 | + return "Invalid month"; |
| 45 | + } |
| 46 | +} |
| 47 | + |
| 48 | + |
| 49 | +console.log(checkSeason("february")); // Winter |
| 50 | +console.log(checkSeason("august")); // Summer |
| 51 | +console.log(checkSeason("October")); // Autumn |
| 52 | +console.log(checkSeason("May")); // Spring |
| 53 | + |
| 54 | +// Question 3 |
| 55 | + |
| 56 | +// With Math.max |
| 57 | + |
| 58 | +// Finding the bigger number |
| 59 | + |
| 60 | +function findMax(a, b, c) { |
| 61 | + return Math.max(a, b, c); |
| 62 | +} |
| 63 | + |
| 64 | + |
| 65 | +console.log(findMax(0, 10, 5)); // 10 |
| 66 | +console.log(findMax(0, -10, -2)); // 0 |
| 67 | + |
| 68 | + |
| 69 | +//Without Math.max |
| 70 | + |
| 71 | +function findMax(a, b, c) { |
| 72 | + let max = a; |
| 73 | + |
| 74 | + if (b > max) { |
| 75 | + max = b; |
| 76 | + } |
| 77 | + |
| 78 | + if (c > max) { |
| 79 | + max = c; |
| 80 | + } |
| 81 | + |
| 82 | + return max; |
| 83 | +} |
| 84 | + |
| 85 | + |
| 86 | +console.log(findMax(0, 10, 5)); // 10 |
| 87 | +console.log(findMax(0, -10, -2)); // 0 |
| 88 | + |
| 89 | + |
| 90 | +// Question 4 |
| 91 | + |
| 92 | +function printArray(arr) { |
| 93 | + for (let i = 0; i < arr.length; i++) { |
| 94 | + console.log(arr[i]); |
| 95 | + } |
| 96 | +} |
| 97 | + |
| 98 | +printArray([1, 2, 3, 4]); |
| 99 | + |
| 100 | + |
| 101 | +// Question 5 |
| 102 | + |
| 103 | +function showDateTime() { |
| 104 | + const now = new Date(); |
| 105 | + |
| 106 | + let day = now.getDate(); |
| 107 | + let month = now.getMonth() + 1; // months start from 0 |
| 108 | + let year = now.getFullYear(); // e.g 2026 |
| 109 | + |
| 110 | + let hours = now.getHours(); |
| 111 | + let minutes = now.getMinutes(); |
| 112 | + |
| 113 | + // add 0 in front if number is 1 digit |
| 114 | + if (day < 10) day = "0" + day; // if day is 5 it becomes 05, 2 becomes 02 and so on |
| 115 | + if (month < 10) month = "0" + month; // above comment in line 114 for line 115-117 |
| 116 | + if (hours < 10) hours = "0" + hours; |
| 117 | + if (minutes < 10) minutes = "0" + minutes; |
| 118 | + |
| 119 | + return `${day}/${month}/${year} ${hours}:${minutes}`; |
| 120 | +} |
| 121 | + |
| 122 | +console.log(showDateTime()); |
| 123 | + |
| 124 | + |
| 125 | +// Question 5 |
| 126 | + |
| 127 | +function printArray(arr) { |
| 128 | + for (let i = 0; i < arr.length; i++) { |
| 129 | + console.log(arr[i]); |
| 130 | + } |
| 131 | +} |
| 132 | + |
| 133 | +printArray([1, 2, 3, 4]); |
| 134 | + |
| 135 | + |
| 136 | + |
0 commit comments