Talentd
FleetCode

2704. To Be Or Not To Be

Easy62.6% acceptance6 companies

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.
5:58
Still stuck?Read 3 approaches, or watch a walkthrough.
Open solution
Loading editor…
1/**
2 * @param {string} val
3 * @return {Object}
4 */
5var expect = function(val) {
6
7};
8
9/**
10 * expect(5).toBe(5); // true
11 * expect(5).notToBe(5); // throws "Equal"
12 */
Starter codeJavaScriptSpaces: 4
Examples
Input
() => expect(5).toBe(5)
Input
() => expect(5).toBe(null)
Input
() => expect(5).notToBe(null)
Your code is saved on this device.Solution