JavaScript Error - concat_1
There was an Error message while entering to recursive function.
Error Message : Uncaught TypeError : reverse(...).concat is not a function
In this case, I want to build an array by using a concat function. But there was an error. So I have to find out what's the problems.
first I have to find which variable is coming in front of the concat. It has to be an array.
When I check the base, it wasn't. it was a number. So I switch to an array.
function reverse(arr) {
const head = arr[0];
if(arr.length === 1){
return arr[0]; // Error
}
else if(arr.length > 1){
return reverse(arr.slice(1)).concat(arr[0]);
}
}
function reverse(arr) {
const head = arr[0];
if(arr.length === 1){
return arr; // Solve the Error
}
else if(arr.length > 1){
return reverse(arr.slice(1)).concat(arr[0]);
}
}
댓글
댓글 쓰기