Debugging - Exercise 3
Question 1: Fix a Trimmed Username Completed
Fix the function to return the trimmed username; strings are immutable.
Expected output: "Ada" (print this with console.log)
function debugTrimmedUsername(name) {
name.trim();
return name;
}
console.log(debugTrimmedUsername(" Ada "));function debugTrimmedUsername(name) {
return name.trim();
}
console.log(debugTrimmedUsername(" Ada "));Question 2: Fix an Object Property Lookup Completed
Fix the function so it reads the supplied key rather than a literal property named key.
Expected output: 9 (print this with console.log)
function debugDynamicObjectKey(record, key) {
return record.key;
}
console.log(debugDynamicObjectKey({"score":9}, "score"));function debugDynamicObjectKey(record, key) {
return record[key];
}
console.log(debugDynamicObjectKey({"score":9}, "score"));Question 3: Fix a Missing Function Return Completed
Fix the helper so the outer function returns the sum of its inputs.
Expected output: 5 (print this with console.log)
function debugHelperReturn(first, second) {
function add(a, b) { a + b; }
return add(first, second);
}
console.log(debugHelperReturn(2, 3));function debugHelperReturn(first, second) {
function add(a, b) { return a + b; }
return add(first, second);
}
console.log(debugHelperReturn(2, 3));Question 4: Fix a Shadowed Counter Completed
Fix the function so the outer count is incremented inside the block and returned.
Expected output: 6 (print this with console.log)
function debugShadowedCounter(start) {
let count = start;
{ let count = start + 1; }
return count;
}
console.log(debugShadowedCounter(5));function debugShadowedCounter(start) {
let count = start;
{ count++; }
return count;
}
console.log(debugShadowedCounter(5));Question 5: Fix an Array Type Check Completed
Fix the check so it returns true only for arrays.
Expected output: true (print this with console.log)
function debugArrayTypeCheck(value) {
return typeof value === "array";
}
console.log(debugArrayTypeCheck([1,2]));function debugArrayTypeCheck(value) {
return Array.isArray(value);
}
console.log(debugArrayTypeCheck([1,2]));Question 6: Fix a NaN Check Completed
Fix the function so it returns true when converting the input to a number produces NaN.
Expected output: true (print this with console.log)
function debugNaNConversion(text) {
return Number(text) === NaN;
}
console.log(debugNaNConversion("pear"));function debugNaNConversion(text) {
return Number.isNaN(Number(text));
}
console.log(debugNaNConversion("pear"));Question 7: Fix a Missing Break Completed
Fix the switch so "red" maps to "stop", "green" to "go", and other inputs to "unknown".
Expected output: "stop" (print this with console.log)
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"));function debugSignalSwitch(color) {
let message;
switch (color) { case "red": message = "stop"; break; case "green": message = "go"; break; default: message = "unknown"; }
return message;
}
console.log(debugSignalSwitch("red"));Question 8: Fix a Reversed Slice Completed
Fix the slice so it returns characters from start inclusive to end exclusive.
Expected output: "bcd" (print this with console.log)
function debugSliceBounds(text, start, end) {
return text.slice(end, start);
}
console.log(debugSliceBounds("abcdef", 1, 4));function debugSliceBounds(text, start, end) {
return text.slice(start, end);
}
console.log(debugSliceBounds("abcdef", 1, 4));Question 9: Fix an Empty String Check Completed
Fix the function so it returns true only when the trimmed text is empty.
Expected output: true (print this with console.log)
function debugEmptyTextCheck(text) {
return text === "";
}
console.log(debugEmptyTextCheck(" "));function debugEmptyTextCheck(text) {
return text.trim() === "";
}
console.log(debugEmptyTextCheck(" "));Question 10: Fix a Division Formula Completed
Fix the function to return the average of two supplied numbers.
Expected output: 4 (print this with console.log)
function debugTwoNumberAverage(first, second) {
return first + second / 2;
}
console.log(debugTwoNumberAverage(2, 6));function debugTwoNumberAverage(first, second) {
return (first + second) / 2;
}
console.log(debugTwoNumberAverage(2, 6));