Debugging - Exercise 2
Question 1: Fix a Loop's Upper Bound Completed
Fix the loop so it returns integers from 1 through limit inclusive.
Expected output: [1,2,3] (print this with console.log)
function debugInclusiveLoop(limit) {
const result = [];
for (let i = 1; i < limit; i++) result.push(i);
return result;
}
console.log(debugInclusiveLoop(3));function debugInclusiveLoop(limit) {
const result = [];
for (let i = 1; i <= limit; i++) result.push(i);
return result;
}
console.log(debugInclusiveLoop(3));Question 2: Fix an Array Sum Completed
Fix the loop so every array number is included in the sum.
Expected output: 9 (print this with console.log)
function debugArraySum(numbers) {
let total = 0;
for (let i = 0; i < numbers.length - 1; i++) total += numbers[i];
return total;
}
console.log(debugArraySum([2,3,4]));function debugArraySum(numbers) {
let total = 0;
for (let i = 0; i < numbers.length; i++) total += numbers[i];
return total;
}
console.log(debugArraySum([2,3,4]));Question 3: Fix an Early Return Completed
Fix the loop so it returns true if any number is negative and false otherwise.
Expected output: true (print this with console.log)
function debugAnyNegative(numbers) {
for (const number of numbers) { if (number < 0) return true; return false; }
return false;
}
console.log(debugAnyNegative([1,-2]));function debugAnyNegative(numbers) {
for (const number of numbers) { if (number < 0) return true; }
return false;
}
console.log(debugAnyNegative([1,-2]));Question 4: Fix a Filter Predicate Completed
Fix the predicate so only numbers strictly greater than the limit are returned.
Expected output: [5] (print this with console.log)
function debugFilterAboveLimit(numbers, limit) {
return numbers.filter((number) => number >= limit);
}
console.log(debugFilterAboveLimit([2,4,5], 4));function debugFilterAboveLimit(numbers, limit) {
return numbers.filter((number) => number > limit);
}
console.log(debugFilterAboveLimit([2,4,5], 4));Question 5: Fix a Mapped Return Completed
Fix the map callback so it returns twice each number.
Expected output: [4,6] (print this with console.log)
function debugMappedDouble(numbers) {
return numbers.map((number) => { number * 2; });
}
console.log(debugMappedDouble([2,3]));function debugMappedDouble(numbers) {
return numbers.map((number) => number * 2);
}
console.log(debugMappedDouble([2,3]));Question 6: Fix Numeric Sorting Completed
Fix the sort so a new array of numbers is returned in ascending numeric order.
Expected output: [1,2,10] (print this with console.log)
function debugNumericSort(numbers) {
return [...numbers].sort();
}
console.log(debugNumericSort([10,2,1]));function debugNumericSort(numbers) {
return [...numbers].sort((a, b) => a - b);
}
console.log(debugNumericSort([10,2,1]));Question 7: Fix a Missing Accumulator Completed
Fix reduce so it returns the sum and works for an empty array.
Expected output: 5 (print this with console.log)
function debugReduceSum(numbers) {
return numbers.reduce((total, number) => total + number, 1);
}
console.log(debugReduceSum([2,3]));function debugReduceSum(numbers) {
return numbers.reduce((total, number) => total + number, 0);
}
console.log(debugReduceSum([2,3]));Question 8: Fix an Array Copy Completed
Fix the function so it returns an object with the original array unchanged and a reversed copy under keys original and reversed.
Expected output: {"original":[1,2,3],"reversed":[3,2,1]} (print this with console.log)
function debugReverseCopy(items) {
const reversed = items.reverse();
return { original: items, reversed };
}
console.log(debugReverseCopy([1,2,3]));function debugReverseCopy(items) {
const reversed = [...items].reverse();
return { original: items, reversed };
}
console.log(debugReverseCopy([1,2,3]));Question 9: Fix a Loop's Starting Index Completed
Fix the loop so it counts every occurrence of the supplied value, including at index zero.
Expected output: 2 (print this with console.log)
function debugCountArrayMatches(items, wanted) {
let count = 0;
for (let i = 1; i < items.length; i++) if (items[i] === wanted) count++;
return count;
}
console.log(debugCountArrayMatches([2,1,2], 2));function debugCountArrayMatches(items, wanted) {
let count = 0;
for (let i = 0; i < items.length; i++) if (items[i] === wanted) count++;
return count;
}
console.log(debugCountArrayMatches([2,1,2], 2));Question 10: Fix a Search Result Index Completed
Fix the search so an item at index zero counts as found.
Expected output: true (print this with console.log)
function debugItemFound(items, wanted) {
return items.indexOf(wanted) > 0;
}
console.log(debugItemFound(["a","b"], "a"));function debugItemFound(items, wanted) {
return items.indexOf(wanted) !== -1;
}
console.log(debugItemFound(["a","b"], "a"));