Why does the error-checking condition in the code fail to properly validate length and width?

function calculateArea(length, width) {
if (length || width <= 0) {
return “Error: Length and width must be positive numbers.”;
}
return length * width;
}

console.log(calculateArea(-5, 10)); // What will this output?

if (length || width <= 0)

Your conditional statement essentially checks, ‘if length exists or if width is smaller or equal to zero’.
You need to specify the condition explicitly for both length and width.

1 Like