Challenge - 5 Problems
Assert Mastery Badge
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ Predict Output
intermediate2:00remaining
What is the output of this assertion code?
Consider the following Node.js code using the assert module. What happens when it runs?
Node.js
import assert from 'assert'; const value = 5; assert.strictEqual(value, 5); console.log('Assertion passed');
Attempts:
2 left
💡 Hint
Check if the values compared by assert.strictEqual are exactly the same.
✗ Incorrect
assert.strictEqual checks if the two values are strictly equal (===). Since value is 5 and compared to 5, the assertion passes and the console.log runs.
❓ Predict Output
intermediate2:00remaining
What error does this assertion code raise?
Look at this Node.js code snippet using the assert module. What error will it throw?
Node.js
import assert from 'assert'; const obj1 = { a: 1 }; const obj2 = { a: 1 }; assert.strictEqual(obj1, obj2);
Attempts:
2 left
💡 Hint
Remember that strictEqual compares object references, not their content.
✗ Incorrect
assert.strictEqual compares if both operands refer to the exact same object. obj1 and obj2 are different objects with same content, so assertion fails.
❓ component_behavior
advanced2:00remaining
What happens when assert.throws is used incorrectly?
Given this code using assert.throws, what is the behavior?
Node.js
import assert from 'assert'; function testFunc() { return 42; } assert.throws(() => testFunc(), Error);
Attempts:
2 left
💡 Hint
assert.throws expects the function to throw an error to pass.
✗ Incorrect
assert.throws checks if the function throws an error. Since testFunc returns a value and does not throw, assert.throws fails and throws an AssertionError.
📝 Syntax
advanced2:00remaining
Which option correctly uses assert.deepStrictEqual?
Which code snippet correctly asserts that two objects with nested properties are deeply equal?
Attempts:
2 left
💡 Hint
Use deepStrictEqual to compare nested objects for exact equality.
✗ Incorrect
assert.deepStrictEqual compares nested objects recursively and checks for strict equality of values. Other methods either do shallow comparison or are deprecated.
🔧 Debug
expert2:00remaining
Why does this assertion code fail unexpectedly?
Examine this code snippet. Why does the assertion fail even though the objects look identical?
Node.js
import assert from 'assert'; const arr1 = [1, 2, 3]; const arr2 = [1, 2, '3']; assert.deepStrictEqual(arr1, arr2);
Attempts:
2 left
💡 Hint
Check the data types of each element in the arrays.
✗ Incorrect
assert.deepStrictEqual checks both value and type. The third element in arr1 is number 3, but in arr2 it is string '3', causing the assertion to fail.