-
Notifications
You must be signed in to change notification settings - Fork 6
Expand file tree
/
Copy pathindex.js
More file actions
57 lines (48 loc) · 1.25 KB
/
index.js
File metadata and controls
57 lines (48 loc) · 1.25 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
// Debugger Practice
// Debug the following functions using console.logs and the VS Code debugger.
function add(a, b) {
// += VS +
// missing return statement
return a + b;
}
function multiply(a, b) {
// b in place of c
return a * b;
}
function divide(a, b) {
// === VS ==
// missing return statement
if (b === 0) {
throw new Error('Division by zero');
}
return a / b;
}
function findMax(arr) {
// 0 is not a good default value as there might be negative numbers in the array
// pick the first element in the array as the default value
// consequently, start the loop at index 1
let max = arr[0];
let i = 1;
while (i < arr.length) {
if (arr[i] > max) {
max = arr[i];
}
i++;
}
return max;
}
function calculateAverage(arr) {
// <= VS <
// we need to divide by all of the elements in the list when calculating the avg
let sum = 0;
let i = 0;
while (i < arr.length) {
sum += arr[i];
i++;
}
return sum / (arr.length);
}
// If you plan on using the VSC debugger don't forget to invoke the function(s) you want to test
// If you don't invoke the function(s), the breakpoint will not be hit
// Example:
// add(2, 3);