-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathDay-47-Debugging-Practice.js
More file actions
207 lines (95 loc) ยท 3.23 KB
/
Copy pathDay-47-Debugging-Practice.js
File metadata and controls
207 lines (95 loc) ยท 3.23 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
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
// Day 47: Debugging Practice
// Practice Time
// 1. Fix the Greeting Function
// This code should greet someone, but it's broken!
function greetPerson(name) {
console.log("Hello, " + name + "!"); // Find & fix the bug
}
greetPerson("Adil");
// Expected: "Hello, Adil!"
// Fixed: the variable name was spelled wrong ๐
// 2. Debug the Sum Function
// This should sum all numbers in an array, but something's wrong
function sumArray(numbers) {
let sum = 0;
// fixed this ๐ to 0
for (let i = 0; i < numbers.length; i++) { // Bug is here!
console.log(`๐ Current iteration: ${i}`);
sum += numbers[i];
}
console.log(`๐งฎ Final sum: ${sum}`);
return sum;
}
console.log(sumArray([10, 20, 30]));
// Expected: 60
// Actual: 50 (it's missing something!)
// Fixed: the loop should start from index 0, not 1 ๐
// 3. Find Multiple Bugs
// This code has 3 bugs! Find and fix them all.
function findLargest(numbers) {
let largest = numbers[0];
// fixed this ๐ to '<'
for (let i = 0; i < numbers.length; i++) { // Bug #1
if (numbers[i] > largest) { // Bug #2 (happens because of #1)
// and ๐ this to '='
largest = numbers[i]; // Bug #3
}
}
return largest;
}
console.log(findLargest([45, 67, 23, 89, 12]));
// Expected: 89
// Fixed:
// #Bug 1: using <= instead of < ๐
// #Bug 2: idk ๐
// #Bug 3: used == instead of = ๐
// 4. Debug with Console Logs
// This code should check if a number is even, but always returns false
function isEven(num) {
// this ๐ should be 'num % 2 === 0' instead of just 'num % 2'
let result = num % 2 === 0;
return result;
}
console.log(isEven(4)); // Expected: true, Getting: 0, because 4 % 2 = 0
console.log(isEven(7)); // Expected: false, Getting: 1, because 7 % 2 = 1
// Fixed: math problem ๐
// 5. The Mysterious Loop
// This should print 1 to 5, but something weird happens
// why tf a semicolon is ๐ here?? ๐
for (let i = 1; i < 6; i++) { // Spot the sneaky bug!
// wtf?? it looks correct ๐
console.log(i);
}
// What happens? Why? Fix it!
// Fixed: removed the semicolon after the for loop declaration ๐
// MEGA CHALLENGE: Debug the Grade Calculator
// This program should calculate average and assign letter grade
// But it has 5 bugs! Find them all!
function calculateGrade(scores) {
// Bug #1: Wrong initial value (it seems okay ๐)
let sum = 0;
// Bug #2: Loop condition
// '<=' instead of '<' again ๐คฆโโ๏ธ๐
for (let i = 0; i < scores.length; i++) {
sum += scores[i];
}
// Bug #3: Missing parentheses (what?? ๐)
let average = sum / scores.length;
let grade;
// Bug #4: Wrong comparison operator
if (average >= 90) {
grade = "A";
} else if (average >= 80) {
grade = "B";
// '=' instead of '>=' here ๐๐
} else if (average >= 70) { // This is the bug!
grade = "C";
} else {
grade = "F";
}
// Bug #5: Returning wrong value
return grade;
}
let testScores = [85, 92, 78, 90];
console.log(calculateGrade(testScores));
// Expected: "B" (average is 86.25)