Q: 3
A developer needs to test this function:
01 const sum3 = (arr) => (
02 if (!arr.length) return 0,
03 if (arr.length === 1) return arr[0],
04 if (arr.length === 2) return arr[0] + arr[1],
05 return arr[0] + arr[1] + arr[2],
06 );
Which two assert statements are valid tests for the function?
Choose 2 answers
Options
Discussion
Its A and C since those assertions are legit JS syntax for console.assert, just checking structure not pass/fail. The others have errors in how the statement's written. Pretty sure that's what "valid test" means here, but I'm open to other takes.
Probably A and C. Both use valid JS syntax for console.assert, with the test expression fully inside the parens. B trips people up since it's comparing outside the assert, and D isn't even valid JS. Not 100% but that's how I see it, correct me if I'm off.
Option A and C
A and C tbh, just looking at the way the assert is structured. Both put the condition directly in console.assert(), matching the correct JS test syntax. The function signature looks like it wants an array param, so yeah they wouldn't pass, but the question only asks for valid test statements not passing ones. Happy to hear if someone spots a subtlety I missed.
A and C imo, but if you look close, sum3 expects an array not separate numbers. So these asserts are valid syntax-wise but wouldn't really work as intended unless the function was fixed or called with an array. Pretty sure that's what the question wants though.
Man, every JS cert loves tricking you with these. It asks about valid assert statement syntax, not whether the test will pass. So it's A and C, both structure the assert call properly. Anyone else frustrated these never actually check if the function works right?
A and C tbh, since they put the comparison inside console.assert which is what JS expects. Pretty sure the function really wants array args so these would fail, but as tests they're still structurally valid. Agree?
A and C imo. Both follow the correct syntax for console.assert in JS, which is clear in this question format.
Be respectful. No spam.