Recent Posts
Recent Comments
게으른개발너D
[Function, Closure] To Be Or Not To Be 본문
https://leetcode.com/problems/to-be-or-not-to-be/description/
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
/**
* @param {string} val
* @return {Object}
*/
var expect = function(val) {
const toBe = (a) => {
if(val === a) {
return true;
} else {
throw "Not Equal";
}
}
const notToBe = (a) => {
if(val !== a) {
return true;
} else {
throw "Equal";
}
}
return { toBe, notToBe };
};
/**
* expect(5).toBe(5); // true
* expect(5).notToBe(5); // throws "Equal"
*/
'알고리즘 > 과제' 카테고리의 다른 글
[Promise] Add Two Promises (feat. all) ⭐️ (0) | 2023.07.04 |
---|---|
[Funtions, Timeout] Execute Cancellable Function With Delay (0) | 2023.07.04 |
[Array] Array Wrapper (method, prototype 만들기) ⭐️ (0) | 2023.07.04 |
[Function, Closure] Allow One Function Call (함수 한 번만 호출하기) (0) | 2023.07.03 |
[Function, Closure, Class] Counter II (closure 함수 여러개 넣기) ⭐️ (0) | 2023.07.03 |
Comments