CRT-600 practice question 3 of 30

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

Full set: 223 questions
Answer options
A: console.assert(sum3(1, ‘2’)) == 12); Correct answer
B: console.assert(sum3(0)) == 0);
C: console.assert(sum3(-3, 2 )) == -1); Correct answer
D: console.assert(sum3(‘hello’, 2, 3, 4)) === NaN);
Correct Answer:
A. console.assert(sum3(1, ‘2’)) == 12);; C. console.assert(sum3(-3, 2 )) == -1);
Explanation

The console.assert() method is used to verify that a given expression is true. The entire expression to be evaluated must be passed as the first argument. The correct syntax for testing a function's return value is console.assert(functionCall() == expectedValue). Options A and C correctly place the entire comparison expression inside the console.assert() call. Although the sum3 function is called with non-array arguments (e.g., sum3(-3, 2) instead of sum3([-3, 2])), causing it to return 0 and the assertions to fail, the statements themselves are structurally valid as tests. A failing assertion is a valid and useful test result.

Why Incorrect

B. console.assert(sum3(0)) == 0);
This statement incorrectly places the comparison == 0 outside the console.assert() call, comparing its return value (undefined) to 0 instead of testing the function's result.
D. console.assert(sum3(‘hello’, 2, 3, 4)) === NaN);
Similar to option B, the comparison === NaN is outside the console.assert() call. This structure does not correctly test the return value of the sum3 function. ---

References

1. MDN Web Docs

console.assert():

Reference: The syntax is console.assert(assertion

...data). The assertion is any boolean expression. If the assertion is false or falsy

a message is written to the console.

Location: MDN Web Docs

console.assert() page

"Syntax" and "Parameters" sections. This source confirms that the entire logical condition must be the first parameter.

2. WHATWG

Console Standard:

Reference: The specification for assert(condition

...data) states: "If condition is true

then return." This implies the entire condition to be evaluated for its truthiness must be the first argument.

Location: WHATWG Console Standard

Section 3.3 assert()

steps 1-3.

3. MDN Web Docs

Functions:

Reference: "If a function is called with more arguments than there are named parameters

the extra arguments are ignored by the named parameters." For sum3(arg1

arg2)

the parameter arr will only receive arg1.

Location: MDN Web Docs

Functions

"Parameters". This supports the analysis of how the function behaves with the provided inputs in options A and C.

Scroll to Top