Jump into concepts and practice - no test required
or
Recommended
Test this pattern10 questions across easy, medium, and hard to know if this pattern is strong
Why debugging skills matter
📖 Scenario: You are building a simple Node.js script that processes a list of numbers to find which are even. Debugging skills help you find and fix mistakes in your code so it works correctly.
🎯 Goal: Create a Node.js script that holds a list of numbers, sets a threshold number, filters the list to keep only even numbers greater than the threshold, and finally exports the filtered list.
📋 What You'll Learn
Create an array called numbers with the values [2, 5, 8, 11, 14]
Create a variable called threshold and set it to 6
Use Array.filter() with a callback that keeps numbers greater than threshold and even
Export the filtered array as filteredNumbers
💡 Why This Matters
🌍 Real World
Filtering data based on conditions is common in real-world apps, like showing only available products or valid user inputs.
💼 Career
Debugging and filtering data are essential skills for Node.js developers working on backend services and APIs.
Progress0 / 4 steps
1
DATA SETUP: Create the numbers array
Create an array called numbers with these exact values: [2, 5, 8, 11, 14]
Node.js
Hint
Use const numbers = [2, 5, 8, 11, 14]; to create the array.
2
CONFIGURATION: Set the threshold value
Create a variable called threshold and set it to the number 6
Node.js
Hint
Use const threshold = 6; to set the threshold.
3
CORE LOGIC: Filter numbers greater than threshold and even
Create a variable called filteredNumbers that uses numbers.filter() with a callback function. The callback should keep numbers greater than threshold and that are even (divisible by 2).
Node.js
Hint
Use numbers.filter(number => number > threshold && number % 2 === 0) to filter the array.
4
COMPLETION: Export the filtered numbers array
Add a line to export filteredNumbers using module.exports.
Node.js
Hint
Use module.exports = { filteredNumbers }; to export the variable.
Practice
(1/5)
1. Why is debugging important when writing Node.js programs?
easy
A. It makes the program run faster automatically.
B. It helps find and fix errors to make the program work correctly.
C. It adds new features to the program without coding.
D. It removes the need to write tests for the program.
Solution
Step 1: Understand the purpose of debugging
Debugging is used to find and fix errors in code so the program runs as expected.
Step 2: Evaluate the options
Only It helps find and fix errors to make the program work correctly. correctly states debugging helps fix errors. Other options describe unrelated benefits.
Final Answer:
It helps find and fix errors to make the program work correctly. -> Option B
Quick Check:
Debugging = Fix errors [OK]
Hint: Debugging = finding and fixing errors quickly [OK]
Common Mistakes:
Thinking debugging improves speed automatically
Confusing debugging with adding features
Believing debugging replaces testing
2. Which of the following is the correct way to print a variable's value for debugging in Node.js?
easy
A. console.log(variable);
B. log.console(variable);
C. print(variable);
D. console.print(variable);
Solution
Step 1: Recall Node.js debugging syntax
In Node.js, console.log() is the standard method to print values to the console.
Step 2: Check each option
Only console.log(variable); uses the correct syntax. Others are invalid or do not exist in Node.js.
Final Answer:
console.log(variable); -> Option A
Quick Check:
Print value = console.log() [OK]
Hint: Use console.log() to print variables in Node.js [OK]
Common Mistakes:
Using console.print() which does not exist
Using print() without console
Swapping console and log order
3. What will be the output of this Node.js code snippet?
const x = 5; console.log(x + y); const y = 3;
medium
A. 8
B. 5undefined
C. ReferenceError: Cannot access 'y' before initialization
D. NaN
Solution
Step 1: Understand variable hoisting with const
Variables declared with const are not hoisted like var. Accessing before declaration causes ReferenceError.
Step 2: Analyze the code execution order
console.log(x + y); runs before y is declared, causing ReferenceError.
Final Answer:
ReferenceError: Cannot access 'y' before initialization -> Option C
Quick Check:
Access const before declaration = ReferenceError [OK]
Hint: const variables cannot be used before declaration [OK]
Common Mistakes:
Assuming y is undefined and prints NaN
Thinking variables are hoisted like var
Expecting output 8 without error
4. You run a Node.js program and get an unexpected error. Which debugging step helps find the exact line causing the error?
medium
A. Add multiple console.log() statements before and after suspect lines.
B. Delete all code and rewrite from scratch.
C. Ignore the error and run the program again.
D. Change variable names randomly.
Solution
Step 1: Understand debugging with console logs
Adding console.log() before and after lines helps track program flow and locate errors.
Step 2: Evaluate other options
Deleting code or ignoring errors does not help find the error. Random renaming causes more issues.
Final Answer:
Add multiple console.log() statements before and after suspect lines. -> Option A
Quick Check:
Use console.log() to trace errors [OK]
Hint: Trace errors with console.log() around problem code [OK]
Common Mistakes:
Ignoring errors hoping they disappear
Deleting code without understanding
Changing variable names without reason
5. You have a Node.js function that sometimes returns undefined unexpectedly. What is the best debugging approach to find why?
hard
A. Comment out the entire function and run the program.
B. Remove all return statements to avoid undefined.
C. Restart the computer to clear errors.
D. Use console.log() to print input and output values at each step inside the function.
Solution
Step 1: Trace function inputs and outputs
Printing inputs and outputs inside the function helps identify where undefined is introduced.
Step 2: Avoid ineffective fixes
Removing returns or commenting out the function hides the problem. Restarting does not fix code logic errors.
Final Answer:
Use console.log() to print input and output values at each step inside the function. -> Option D
Quick Check:
Trace values inside function with console.log() [OK]
Hint: Log inputs and outputs inside functions to find undefined [OK]