Skip to content

Commit 55f4593

Browse files
author
Anna Aitchison
committed
Add debugging
1 parent d19fa0c commit 55f4593

7 files changed

Lines changed: 104 additions & 0 deletions

File tree

‎debugging/bank/1.js‎

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
/*
2+
This function should check to see if the given number is equal to
3+
the number 5. This function isn't working correctly can you see why this is the case?
4+
And fix this code!
5+
*/
6+
7+
function isValueFive(n) {
8+
if (n === "5") {
9+
return true
10+
}
11+
12+
return false
13+
}
14+
15+
isValueFive(5)
16+

‎debugging/bank/2.js‎

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
/*
2+
This function should return the value of the largest number in the given array.
3+
Can you fix the bug in this function
4+
5+
*/
6+
7+
function getLargestNum(arr) {
8+
let largestNum = 0;
9+
for (let i = 0; i <= arr.length; i++) {
10+
if (arr[i] > largestNum) {
11+
largestNum = arr[i];
12+
}
13+
}
14+
return largestNum;
15+
}
16+
let arr = [1, 2, 3, 4, 5];
17+
18+
getLargestNum(arr)

‎debugging/bank/3.js‎

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
/*
2+
There is a bug in this code which may not be immediately obvious.
3+
Hint: Try different test cases to uncover the bug. Then update the code to
4+
resolve the bug.
5+
*/
6+
7+
function calculateAverage(arr) {
8+
let sum = 0;
9+
for (let i = 0; i < arr.length; i++) {
10+
sum += arr[i];
11+
}
12+
return sum / arr.length;
13+
}
14+
15+
let arr = [1, 2, 3, 4, 5];
16+
console.log(calculateAverage(arr));

‎debugging/bank/4.js‎

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
/*
2+
This function should return the sum of all the numbers in the array.
3+
Can you spot why this function is not working and resolve.
4+
*/
5+
6+
function calculateSum(arr) {
7+
let sum = 0;
8+
for (let i = 0; i <= arr.length; i++) {
9+
sum += arr[i];
10+
}
11+
return sum;
12+
}
13+
14+
let arr = [1, 2, 3, 4, 5];
15+
let sum = calculateSum(arr);
16+
console.log("The sum of " + arr + " is " + sum);

‎debugging/bank/5.js‎

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
/*
2+
This code contains more than one error. Can you find and fix the errors.
3+
*/
4+
5+
let list1 = [1, 2, 3, 4, 5];
6+
let list2 = [6, 7, 8, 9, 10];
7+
8+
function mergeLists(list1, list2) {
9+
let mergedList = [];
10+
for (let i = 0; i < list1.length; i++) {
11+
mergedList.push(list1[i]);
12+
for (let j = 0; j < list2.length; i++) {
13+
mergedList.push(list2[j]);
14+
}
15+
}
16+
return mergedList;
17+
}
18+
19+
//Expected output: [1,6,2,7,3,8,4,9,5,10]
20+
console.log(mergeLists(list1, list2));

‎debugging/bank/findLargest.js‎

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
const findLargest = (numbers) => {
2+
let largest = numbers[0];
3+
for (let i = 1; i < numbers.length; i++) {
4+
if (numbers[i] > largest) {
5+
largest = numbers[i];
6+
}
7+
}
8+
return largest;
9+
};
10+
console.log(findLargest([3, 7, 2, 5, 6]));

‎debugging/bank/sumArray.js‎

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
const sumArray = (numbers) => {
2+
let total = 0;
3+
for (let i = 0; i < numbers.length; i++) {
4+
total += numbers[i];
5+
}
6+
return total;
7+
};
8+
console.log(sumArray([1, 2, 3]));

0 commit comments

Comments
 (0)