2704. To Be Or Not To Be
Description
Write a functionΒ expect that helps developers test their code. It should take in any valueΒ valΒ and return an object with the following two functions.
toBe(val)Β accepts another value and returnsΒtrueΒ if the two valuesΒ===Β each other. If they are not equal, it should throw an errorΒ"Not Equal".notToBe(val)Β accepts another value and returnsΒtrueΒ if the two valuesΒ!==Β each other. If they are equal, it should throw an errorΒ"Equal".
Β
Example 1:
Input: func = () => expect(5).toBe(5)
Output: {"value": true}
Explanation: 5 === 5 so this expression returns true.
Example 2:
Input: func = () => expect(5).toBe(null)
Output: {"error": "Not Equal"}
Explanation: 5 !== null so this expression throw the error "Not Equal".
Example 3:
Input: func = () => expect(5).notToBe(null)
Output: {"value": true}
Explanation: 5 !== null so this expression returns true.
Solutions
Solution 1
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 | |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 | |