B. It uses strict equality instead of loose equality
C. It does not stop execution after sending error response
D. It should use res.json() instead of res.send()
Solution
Step 1: Analyze the error handling flow
The code sends a 400 error but does not return or stop, so it continues to send success response.
Step 2: Identify the fix
Adding 'return' before res.status(400).send(...) stops further execution.
Final Answer:
It does not stop execution after sending error response -> Option C
Quick Check:
Missing return causes double response [OK]
Hint: Return after sending error to stop code [OK]
Common Mistakes:
Ignoring missing return after res.send()
Confusing equality checks with flow control
Thinking res.json() is required for errors
5. You want to validate that req.body.age is a number greater than 18 before processing. Which code snippet correctly validates this and sends a 400 error if invalid?
hard
A. if (!req.body.age || typeof req.body.age !== 'number' || req.body.age <= 18) {
return res.status(400).send('Age must be a number over 18');
}
B. if (req.body.age <= 18) {
res.status(400).send('Age must be over 18');
}
C. if (typeof req.body.age === 'string' && req.body.age > 18) {
return res.status(400).send('Invalid age');
}
D. if (!req.body.age || req.body.age < 18) {
res.send('Age is valid');
}
Solution
Step 1: Check for presence and type of age
Code verifies age exists and is a number using typeof.
Step 2: Check age value is greater than 18
It ensures age is over 18, else sends 400 error with message.
Step 3: Confirm proper use of return to stop execution
Return stops further processing after error response.
Final Answer:
if (!req.body.age || typeof req.body.age !== 'number' || req.body.age <= 18) {
return res.status(400).send('Age must be a number over 18');
} -> Option A