Debugging - Exercise 1
Question 1: Fix a Numeric String Sum Completed
Fix the function so it converts the numeric text before adding it to the number.
Expected output: 15 (print this with console.log)
function debugNumericTextSum(number, text) {
return number + text;
}
console.log(debugNumericTextSum(7, "8"));function debugNumericTextSum(number, text) {
return number + Number(text);
}
console.log(debugNumericTextSum(7, "8"));Question 2: Fix an Inclusive Minimum Completed
Fix the comparison so values equal to the minimum also pass.
Expected output: true (print this with console.log)
function debugInclusiveMinimum(value, minimum) {
return value > minimum;
}
console.log(debugInclusiveMinimum(5, 5));function debugInclusiveMinimum(value, minimum) {
return value >= minimum;
}
console.log(debugInclusiveMinimum(5, 5));Question 3: Fix a Last Array Item Completed
Fix the index so the function returns the final item of a non-empty array.
Expected output: "b" (print this with console.log)
function debugLastArrayItem(items) {
return items[items.length];
}
console.log(debugLastArrayItem(["a","b"]));function debugLastArrayItem(items) {
return items[items.length - 1];
}
console.log(debugLastArrayItem(["a","b"]));Question 4: Fix a Rectangle Area Completed
Fix the arithmetic so the function returns rectangle area.
Expected output: 15 (print this with console.log)
function debugRectangleArea(width, height) {
return width + height;
}
console.log(debugRectangleArea(3, 5));function debugRectangleArea(width, height) {
return width * height;
}
console.log(debugRectangleArea(3, 5));Question 5: Fix a Fahrenheit Conversion Completed
Fix the formula for converting Celsius to Fahrenheit.
Expected output: 32 (print this with console.log)
function debugFahrenheitConversion(celsius) {
return celsius * 5 / 9 + 32;
}
console.log(debugFahrenheitConversion(0));function debugFahrenheitConversion(celsius) {
return celsius * 9 / 5 + 32;
}
console.log(debugFahrenheitConversion(0));Question 6: Fix an Assignment in a Condition Completed
Fix the condition so it compares the supplied status with "ready" instead of changing it.
Expected output: false (print this with console.log)
function debugStatusComparison(status) {
return (status = "ready") === "ready";
}
console.log(debugStatusComparison("waiting"));function debugStatusComparison(status) {
return status === "ready";
}
console.log(debugStatusComparison("waiting"));Question 7: Fix Strict Code Comparison Completed
Fix the comparison so values with different types are not considered equal.
Expected output: false (print this with console.log)
function debugStrictCodeComparison(first, second) {
return first == second;
}
console.log(debugStrictCodeComparison(5, "5"));function debugStrictCodeComparison(first, second) {
return first === second;
}
console.log(debugStrictCodeComparison(5, "5"));Question 8: Fix a Default for Zero Completed
Fix the function so it uses 10 only for null or undefined and preserves zero.
Expected output: 0 (print this with console.log)
function debugNullishDefault(value) {
return value || 10;
}
console.log(debugNullishDefault(0));function debugNullishDefault(value) {
return value ?? 10;
}
console.log(debugNullishDefault(0));Question 9: Fix a Returned Object Completed
Fix the arrow function so it returns an object with a value property.
Expected output: {"value":3} (print this with console.log)
function debugArrowObjectReturn(input) {
const make = (value) => { value: value };
return make(input);
}
console.log(debugArrowObjectReturn(3));function debugArrowObjectReturn(input) {
const make = (value) => ({ value });
return make(input);
}
console.log(debugArrowObjectReturn(3));Question 10: Fix a Boolean Toggle Completed
Fix the function so it returns the opposite of the supplied boolean.
Expected output: false (print this with console.log)
function debugBooleanToggle(enabled) {
return enabled;
}
console.log(debugBooleanToggle(true));function debugBooleanToggle(enabled) {
return !enabled;
}
console.log(debugBooleanToggle(true));