[30 Days of JS] To be or Not to be / Counter II
2025. 5. 8. 10:21ㆍ개인활동/코테
반응형
/**
* @param {string} val
* @return {Object}
*/
var expect = function(val) {
return {
toBe: function(expected) {
if (val === expected) {
return true;
} else {
throw new Error("Not Equal");
}
},
notToBe: function(expected) {
if (val !== expected) {
return true;
} else {
throw new Error("Equal");
}
}
};
};
/**
* expect(5).toBe(5); // true
* expect(5).notToBe(5); // throws "Equal"
*/
- toBe 함수와 notToBe 함수에는 각각 파라미터가 하나 더 들어감 -> function(expected)
- throw new Error : 에러 띄우기
- Java Script는 "==="가 "=="와 같은 의미, "!=="가 "!="와 같은 의미
/**
* @param {integer} init
* @return { increment: Function, decrement: Function, reset: Function }
*/
var createCounter = function(init) {
let current = init;
return {
reset: function() {
current = init;
return current;
},
increment: function(){
return ++current;
},
decrement: function(){
return --current;
}
};
};
/**
* const counter = createCounter(5)
* counter.increment(); // 6
* counter.reset(); // 5
* counter.decrement(); // 4
*/
- return 시 빼거나 더하기가 완료 된 상태여야 함 -> current 앞에 연산자 붙이기
- let variable : 해당 함수 내에서만 사용되는 변수 선언
https://leetcode.com/studyplan/30-days-of-javascript/
반응형
'개인활동 > 코테' 카테고리의 다른 글
[30 Days of JS] Array reduce transformation / Function composition (0) | 2025.05.10 |
---|---|
[30 Days of JS] Apply Transform Over Each Element in Array / Filter Elements from Array (0) | 2025.05.09 |
[30 Days of JS] Create Hello World Function / Counter (0) | 2025.05.07 |
[LeetCode/MySQL50] 577. Employee Bonus (0) | 2025.03.10 |
[LeetCode/MySQL50] 1661. Average Time of Process per Machine (0) | 2025.02.03 |