How could i fix in Recursion code in javascript

my code is like that. it was work at first.. not more

function HA7(arr, id) { 
  for (let i = 0; i < arr.length; i++) { 
    if(arr[i]['id']=== id) {
      return arr[i]
    }
      else if(arr[i]=== undefined && arr[i][id] === id  ) {
      return arr[i][id]
    }
      else if(arr[i][id] === undefined){
      return null ;
    }
    return HA7(arr[i],id)
  }
}

I wanna more continue work by recursive function .. but once agian only return null.
(i wanna catch case id value null or arr is undefined)

// example code.

let arr = [
  {
    id: 1,
    name: 'johnny',
  },
  {
    id: 2,
    name: 'ingi',
    children: [
      {
        id: 3,
        name: 'johnson',
      },
      {
        id: 5,
        name: 'steve',
        children: [
          {
            id: 6,
            name: 'lisa',
          },
        ],
      },
      {
        id: 11,
      },
    ],
  },
  {
    id: '13',
  },
];

let output = HA7(arr, 1);
console.log(output); // --> { id: 1, name: 'johnny' }

output = HA7(arr, 5);
console.log(output); // --> { id: 5, name: 'steve', children: [{ id: 6, name: 'lisa' }] }

output = HA7(arr, 99);
console.log(output); // --> null

How should I fix it more? please tell me a some tips ..

1 thought on “How could i fix in Recursion code in javascript”

  1. transformation 1

    Replacing for(;;) loop with for..of

    function HA7(arr, id) { 
      for (const x of arr) { 
        if(x.id === id) {
          return x
        }
        else if(x=== undefined && x[id] === id  ) {
          return x[id]
        }
        else if(x[id] === undefined){
          return null ;
        }
        return HA7(x,id)
      }
    }
    

    It’s much easier to see what’s going on, thanks to replacing all arr[i] with a simple x. We also changed x["id"] to a simple x.id.

    1. Now we can see other problems. You are testing x.id and x[id]before checking ifxisnullorundefined`.

    2. You make a check like x === undefined && x[id] === id that will always be false. This is because if x is undefined, the && x[id] will not only be false, it will also throw an error because you are trying to lookup a property an a null object

    3. You are writing x[id] where you probably mean to write x.id

    transformation 2

    function HA7(arr, id) { 
      for (const x of arr) {
        if (x == null)
          continue
        else if (x.id == id)
          return x
        else
          for (const child of x.children)
          { const result = HA7(child, id)
            if (result != null)
              return result
          }
      }
    }
    

    transformation 3

    Using generators we can do this even better

    function* HA7 (arr, id)
    { for (const x of arr)
      { if (x == null) continue
        if (x.id == id) yield x
        if (x.children) yield *HA7(x.children, id)   
      }
    }
    

    I just wrote an post that is very similar to this question. Please see this Q&A for additional explanation and other advantages of using generators for this kind of problem.

    Reply

Leave a Comment