i need a help how can i treat maximum call stack?

Thanks i fixed some sentence by advice. my code is like that,

i wanna find object with id. but if not, I want to return ‘null’

    function ha7(arr, id) {  // i wanna find object with id 

    let result = [];
    for(let i = 0 ; i < arr.length ; i++) {
      if(arr[i].id === id) {   
        return arr[i]   // found id, then return included object
      }
    else if(Array.isArray(arr[i].children)){ // but , its array
       // let ar = ha7(arr[i].children, id)
        result.push(...arr[i].children)      // i put 'arr[i].children' to variables 
      } 
    }
      if (result.id === id) {
        return result                      //  find object with id in inner
 
      } else {
        return ha7(result, id)             // cant find. then go ahead! 
      }
    return null                            // all of none exist id is return null
    }

it is testing array.

    let input = [
      {
        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',
      },
    ];

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

    output = ha7(input, 99);
    console.log(output); // --> null

I tried a lot of trial, like that. i wanna know.
how can i treat maximum call stack ?
and i wanna return ‘null’ value.

function ha7(arr, id) {  // i wanna find object with id 

    let result = [];
    for(let i = 0 ; i < arr.length ; i++) {
      if(arr[i].id === id) {   
        return arr[i]   // found id, then return included object
      }
    else if(Array.isArray(arr[i].children)){ // but , its array
       // let ar = ha7(arr[i].children, id)
        result.push(...arr[i].children)      // i put 'arr[i].children' to variables 
      } 
    }
      if (result.id === id) {
        return result                      //  find object with id in inner
 
      } else {
        return ha7(result, id)             // cant find. then go ahead! 
      }
    return null                            // all of none exist id is return null
    }
    
    let input = [
      {
        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',
      },
    ];

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

    output = ha7(input, 99);
    console.log(output); // --> null

32 thoughts on “i need a help how can i treat maximum call stack?”

  1. 456005 528094Hi. Cool post. Theres an concern together with your site in firefox, and you may want to check this The browser will be the market chief and a good section of men and women will pass more than your fantastic writing because of this difficulty. 160463

    Reply

Leave a Comment