10
이 스 니펫을 실행할 때마다 반환 전에 console.log는 값 20의 20 배 배열을 반환합니다. 그러나 console.log (Check (users, 0, 20)); 'undefined'만 반환합니다.재귀 함수의 반환 값은 'undefined'입니다.
내가 뭘 잘못하고 있니?
var users = [23, 23, 23, 23, 23, 23, 23, 23, 23, 23];
console.log(Check(users, 0, 20));
function Check(ids, counter, limit){
ids.push(23);
// Recursion
if (counter+1 < limit){
Check(ids, counter+1, limit);
}
else {
console.log(ids);
return ids;
}
}
'if' 블록의'return' 문은'undefined'를 의미하지 않습니다. 함수 끝에 하나의 return 문을 넣고'if' 문에 따라 반환 할 값을 설정하면 유지 보수가 더 쉬울 수도 있습니다. – Ian