2024/4/22 leetcode challenge 2day [To Be Or Not To Be]

2024/04/23に公開

やった問題

私の回答

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

function expect(val: any): ToBeOrNotToBe {
    function toBe(except){
        if(val === except){
            return true
        }else{
            throw Error("Not Equal")
        }
    }

    function notToBe(except){
        if(val !== except){
            return true
        }else{
            throw Error("Equal")
        }
    }

    return {
        toBe,
        notToBe
    }
};

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

学び

  • 昨日のclosureが役に立った。

Discussion