Debugging - Exercise 3

Question 1: Fix a Trimmed Username

Fix the function to return the trimmed username; strings are immutable.

Expected output: "Ada" (print this with console.log)

javascript

function debugTrimmedUsername(name) {
  name.trim();
  return name;
}

console.log(debugTrimmedUsername("  Ada  "));

Question 2: Fix an Object Property Lookup

Fix the function so it reads the supplied key rather than a literal property named key.

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

javascript

function debugDynamicObjectKey(record, key) {
  return record.key;
}

console.log(debugDynamicObjectKey({"score":9}, "score"));

Question 3: Fix a Missing Function Return

Fix the helper so the outer function returns the sum of its inputs.

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

javascript

function debugHelperReturn(first, second) {
  function add(a, b) { a + b; }
  return add(first, second);
}

console.log(debugHelperReturn(2, 3));

Question 4: Fix a Shadowed Counter

Fix the function so the outer count is incremented inside the block and returned.

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

javascript

function debugShadowedCounter(start) {
  let count = start;
  { let count = start + 1; }
  return count;
}

console.log(debugShadowedCounter(5));

Question 5: Fix an Array Type Check

Fix the check so it returns true only for arrays.

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

javascript

function debugArrayTypeCheck(value) {
  return typeof value === "array";
}

console.log(debugArrayTypeCheck([1,2]));

Question 6: Fix a NaN Check

Fix the function so it returns true when converting the input to a number produces NaN.

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

javascript

function debugNaNConversion(text) {
  return Number(text) === NaN;
}

console.log(debugNaNConversion("pear"));

Question 7: Fix a Missing Break

Fix the switch so "red" maps to "stop", "green" to "go", and other inputs to "unknown".

Expected output: "stop" (print this with console.log)

javascript

function debugSignalSwitch(color) {
  let message;
  switch (color) { case "red": message = "stop"; case "green": message = "go"; break; default: message = "unknown"; }
  return message;
}

console.log(debugSignalSwitch("red"));

Question 8: Fix a Reversed Slice

Fix the slice so it returns characters from start inclusive to end exclusive.

Expected output: "bcd" (print this with console.log)

javascript

function debugSliceBounds(text, start, end) {
  return text.slice(end, start);
}

console.log(debugSliceBounds("abcdef", 1, 4));

Question 9: Fix an Empty String Check

Fix the function so it returns true only when the trimmed text is empty.

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

javascript

function debugEmptyTextCheck(text) {
  return text === "";
}

console.log(debugEmptyTextCheck("   "));

Question 10: Fix a Division Formula

Fix the function to return the average of two supplied numbers.

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

javascript

function debugTwoNumberAverage(first, second) {
  return first + second / 2;
}

console.log(debugTwoNumberAverage(2, 6));