Skip to content

Latest commit

 

History

History
84 lines (67 loc) · 1.53 KB

_2704. To Be Or Not To Be.md

File metadata and controls

84 lines (67 loc) · 1.53 KB

All prompts are owned by LeetCode. To view the prompt, click the title link above.

Back to top


First completed : July 09, 2024

Last updated : July 10, 2024


Related Topics : N/A

Acceptance Rate : 62.52 %


Solutions

JavaScript

class helper {
    constructor(val) {
        this.val = val;
    }

    toBe(n) {
        if (n !== this.val) throw new Error('Not Equal');
        return true;
    }

    notToBe(n) {
        if (n === this.val) throw new Error('Equal');
        return true;
    }
}


/**
 * @param {string} val
 * @return {Object}
 */
var expect = function(val) {
    return new helper(val);
};

/**
 * expect(5).toBe(5); // true
 * expect(5).notToBe(5); // throws "Equal"
 */
/**
 * @param {string} val
 * @return {Object}
 */
var expect = function(val) {
    let obj = {
        toBe: function(n) {
            if (n !== val) throw new Error('Not Equal');
            return true;
        },
        notToBe: function(n) {
            if (val === n) throw new Error('Equal');
            return true;
        }
    }
    return obj;
};

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