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]);
      }
      
    }







댓글

이 블로그의 인기 게시물

Lesson 12_1 프로퍼티 노드(Property Node)

DAQ로 전압 측정하기-2

Lesson 12_2 참조를 이용한 프로퍼티노드(Property Node)