Debugging - Exercise 2

Question 1: Fix a Loop's Upper Bound

Fix the loop so it returns integers from 1 through limit inclusive.

Expected output: [1,2,3] (print this with console.log)

javascript

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

Fix the loop so every array number is included in the sum.

Expected output: 9 (print this with console.log)

javascript

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]));

Question 3: Fix an Early Return

Fix the loop so it returns true if any number is negative and false otherwise.

Expected output: true (print this with console.log)

javascript

function debugAnyNegative(numbers) {
  for (const number of numbers) { if (number < 0) return true; return false; }
  return false;
}

console.log(debugAnyNegative([1,-2]));

Question 4: Fix a Filter Predicate

Fix the predicate so only numbers strictly greater than the limit are returned.

Expected output: [5] (print this with console.log)

javascript

function debugFilterAboveLimit(numbers, limit) {
  return numbers.filter((number) => number >= limit);
}

console.log(debugFilterAboveLimit([2,4,5], 4));

Question 5: Fix a Mapped Return

Fix the map callback so it returns twice each number.

Expected output: [4,6] (print this with console.log)

javascript

function debugMappedDouble(numbers) {
  return numbers.map((number) => { number * 2; });
}

console.log(debugMappedDouble([2,3]));

Question 6: Fix Numeric Sorting

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)

javascript

function debugNumericSort(numbers) {
  return [...numbers].sort();
}

console.log(debugNumericSort([10,2,1]));

Question 7: Fix a Missing Accumulator

Fix reduce so it returns the sum and works for an empty array.

Expected output: 5 (print this with console.log)

javascript

function debugReduceSum(numbers) {
  return numbers.reduce((total, number) => total + number, 1);
}

console.log(debugReduceSum([2,3]));

Question 8: Fix an Array Copy

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)

javascript

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

Fix the loop so it counts every occurrence of the supplied value, including at index zero.

Expected output: 2 (print this with console.log)

javascript

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));

Question 10: Fix a Search Result Index

Fix the search so an item at index zero counts as found.

Expected output: true (print this with console.log)

javascript

function debugItemFound(items, wanted) {
  return items.indexOf(wanted) > 0;
}

console.log(debugItemFound(["a","b"], "a"));