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 questionsThe 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.
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. ---
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.
Keep practicing
Take the free CRT-600 practice test
Ten exam-style questions with answers and explanations, plus the exam facts and study guides.
More questions
Other CRT-600 practice questions
- Question 2A developer has the function, shown below, that is called when a page loads. function onl…
- Question 4Refer to the code below: Const resolveAfterMilliseconds = (ms) => Promise.resolve ( setTi…
- Question 10Refer to the following code block: class Animal{ constructor(name){ this.name = name; } m…
- Question 11A developer needs to debug a Node.js web server because a runtime error keeps occurring a…
- Question 12Refer to the string below: const str = 'Salesforce'; Which two statements result in the w…
- Question 14Given the code below: Which three code segments result in a correct conversion from numbe…
