← Back to list

Day 3: leetcode — JavaScript 30 Days challenge

2704. To Be Or Not To Be

yuni · 2025-11-13 01:08 · 0 claps · 0.9 min read
#leetcode #leetcode-easy #leetcode-js #javascript #coding-test
Open on Medium ↗
Wiki topics: 💻 · Programming 🌐 · Web Development

Day 3: leetcode — JavaScript 30 Days challenge

2704. To Be Or Not To Be

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.

Solution:


var expect = function(val) {
    return{
        toBe: function(n){
            if(val===n)
                return true;
            else
                throw new Error("Not Equal");

        },
        notToBe: function(n){
            if(val!==n)
                return true;
            else
                throw new Error("Equal")

        }
    }
};

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

The part

return {
    toBe: ...,
    notToBe: ...
}

is returning an object literal — that means expect() returns an object that contains two methods: toBe and notToBe.

{
    toBe: function(n) { ... },
    notToBe: function(n) { ... }
}
  • Then you choose which one to call based on what you write next.

메타데이터
post_id
5c3d17490f27
slug
day-3-leetcode-javascript-30-days-challenge-5c3d17490f27
url
https://medium.com/@gimyunhe/day-3-leetcode-javascript-30-days-challenge-5c3d17490f27
canonical_url
https://medium.com/@gimyunhe/day-3-leetcode-javascript-30-days-challenge-5c3d17490f27
author_url
https://medium.com/@gimyunhe
status
ok
fetched_at
2026-06-20 20:29:01