LeetCode solutions

2813. To Be Or Not To Be

My accepted JavaScript solution to LeetCode problem 2813, To Be Or Not To Be, running in 39ms.

  • Difficulty: Easy
  • JavaScript
  • Runtime 39ms
  • Memory 53.9MB

Read the problem on LeetCode View on GitHub

JavaScript

Accepted on LeetCode — runtime 39ms, memory 53.9MB, accepted 2025-12-31.

javascript
/**
 * @param {string} val
 * @return {Object}
 */
var expect = function(val) {
    return {
        toBe: function(val2) {
            if (val === val2) return true;
            throw new Error("Not Equal");
        },
        notToBe: function(val2) {
            if (val !== val2) return true;
            throw new Error("Equal");
        }
    };
};

/**
 * expect(5).toBe(5); // true
 * expect(5).notToBe(5); // throws "Equal"
 */

Source