LeetCode-in-Java

2704. To Be Or Not To Be

Easy

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

type ToBeOrNotToBe = {
    toBe: (val: any) => boolean
    notToBe: (val: any) => boolean
}

const expect = (val: any): ToBeOrNotToBe => ({
    toBe: (equality: any) => {
        if (val !== equality) {
            throw new Error('Not Equal')
        }
        return true
    },
    notToBe: (equality: any) => {
        if (val === equality) {
            throw new Error('Equal')
        }
        return true
    },
})

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

export { expect }