2704. To Be Or Not To Be

Difficulty:
Related Topics:
    Similar Questions:

      Problem

      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.

      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.
      

      Solution (Javascript)

      /**
       * @param {string} val
       * @return {Object}
       */
      var expect = function (val) {
        return {
          toBe: (val2) => {
            if (val !== val2) throw new Error("Not Equal");
            else return true;
          },
          notToBe: (val2) => {
            if (val === val2) throw new Error("Equal");
            else return true;
          },
        };
      };
      
      /* For example, when expect(5).toBe(4) is called,
         val is the expect parameter (so val equals 5),
         val2 is the toBe parameter (so val2 equals 4).
         Since val !== val2, aka 5 != 4, we throw error "Not Equal". */
      

      Explain:

      nope.

      Complexity: