오늘 재귀함수 코드를 살펴보다가 이상한 표현을 발견했다.
(가위바위보의 경우의 수를 계산하는 알고리즘이다.)
const rockPaperScissors = (rounds) => {
//if rounds is 0 we will return an empty array
if(rounds === 0) return []
// define a throw options array for each play we could make
// "R" represents "Rock", "P" represents "Paper", "S" represents "Scissors"
const throwOptions = ["R", "P", "S"]
// define a solutions array to hold all of our possible solutions
const solutions = []
const combinations = (solution = '') => {
//base case definition
if(solution.length === rounds){
return solutions.push(solution)
}
throwOptions.forEach(option => {
combinations(solution + option)
})
}
combinations()
// return the solutions array
return solutions
}
위에 보면 const combinations = (solution = '') => 라는 표현이 있다.
이게 뭐지?!

이건 매개변수의 기본값을 설정하는 방법으로 ES6에서 추가된 기능이라고 한다.
즉 함수의 인자로 변수 solution을 넘겨주는데, Default값을 ' ' 로 설정한다는 의미다.
추후 다른 값이 solution에 할당된다면 Default값이 아닌 새로 할당된 값이 함수인자로 전달된다.
'Javascript' 카테고리의 다른 글
JS Operator "!!" & "^=" (0) | 2021.11.12 |
---|---|
JS Linked-list & Hash table (0) | 2021.11.11 |
JS Tail recursion (0) | 2021.11.10 |
JS Recursion memory leak (0) | 2021.11.10 |
JS this 바인딩 (0) | 2021.11.08 |