# Leetcode - To Be Or Not To Be

Yuni·2023년 8월 10일
0

목록 보기
15/27

## Problem

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.


## Approach

I compared a value 1 to a value 2, and created two function called 'toBe' and 'notToBe'. In each functions, throw Error if the value 1 is/isn't equal to the value 2, otherwise return true.

1. Make two functions toBe and notToBe. Both function has value2 to compare to the value in main function.
2. toBe: if val isn't equal to the val2, throws Error. else, return true.
notToBe: if val is equal to the val2, throws Error. else, return true.
3. cover two function with return.

### code

/**
* @param {string} val
* @return {Object}
*/
var expect = function(val) {
return {
toBe: (val2) => {
if(val!==val2) {
throw new Error("Not Equal");
}
return true;
},
notToBe: (val2) => {
if(val===val2) {
throw new Error("Equal");
}
return true;
}
}
};
Look at art, make art, show art and be art. So does as code.