문제

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".

 

예제

 

풀이

/**
 * @param {string} val
 * @return {Object}
 */
var expect = function(val) {
    return {
        toBe: function(compVal) {
            if (val === compVal) {
                return true
            } else throw new Error('Not Equal')
        },
        
        notToBe: function(compVal) {
            if (val === compVal) {
                throw new Error('Equal')
            } else return true
        }
    }
};

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

처음 주어지는 값인 val과 메소드 내에 주어지는 값인 compVal에 대하여 비교할 것인데, toBe 메소드의 경우 val 값과 compVal 값이 같다면 true를 반환하고, 그렇지 않을 시 'Not Equal'이라는 메세지와 함께 에러를 반환합니다. notToBe의 경우 val 값과 compVal 값이 다를 경우 true를 반환하고, 같을 시에는 'Equal'이라는 메세지와 함께 에러를 반환합니다.

'Algorithm' 카테고리의 다른 글

[LeetCode] 2715. Timeout Cancellation  (0) 2024.02.05
[LeetCode] 2695. Array Wrapper  (0) 2024.01.31
[LeetCode] 2677. Chunk Array  (0) 2024.01.31
[BOJ] 4779. 칸토어 집합  (1) 2024.01.30
[SWEA] 1230. 암호문3  (1) 2024.01.29

+ Recent posts