Algorithm
30 Days of JavaScript
To Be or Not to Be

2704. To Be Or Not To Be

Tags

  • Closures

Link

https://leetcode.com/problems/to-be-or-not-to-be/description/?envType=study-plan-v2&envId=30-days-of-javascript (opens in a new tab)

Question

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.

Answer

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;
      }
    },
  };
};
/**
 * @param {string} val
 * @return {Object}
 */
var expect = function (val) {
  var toBe = function (...args) {
    if (val !== args[0]) {
      throw new Error("Not Equal");
    }
    if (val == args[0]) {
      return true;
    }
  };
  var notToBe = function (value) {
    if (val === value) {
      throw Error("Equal");
    }
    return true;
  };
  return { toBe, notToBe };
};