How could i fix in Recursion code in javascript

Loolii

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 ..

Thank you

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. Now we can see other problems.

  1. You are testing x.id and x[id] before checking if x is null or undefined.

  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

  4. You are writing return null before we have exhausted the search


transformation 2

Let's fix all of the problems we identified above -

function HA7(arr, id) { 
  for (const x of arr) {
    if (x == null)           // <- check nulls first, always
      continue               // <- don't return yet
    else if (x.id == id)          // <- or when id matches
      return x                    // <- return x
    else                                  // <- otherwise
      for (const child of HA7(x.children, id)) // <- search children
        if (result != null)                    // <- if result is found
          return result                        // <- return it
  }
}

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

function first (t)
{ for (const x of t)
    return x
}

const 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'}]

console.log(first(HA7(input, 1)))
console.log(first(HA7(input, 5)))
console.log(first(HA7(input, 99)))

{
  "id": 1,
  "name": "johnny"
}

{
  "id": 5,
  "name": "steve",
  "children": [
    {
      "id": 6,
      "name": "lisa"
    }
  ]
}

undefined

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.

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

Something is wrong with my HTML code, how could I fix it?

How to fix the recursion problem in jQuery code

Recursion seems to iterate one last time how can I fix this code?

How could I simplify this code? JavaScript + css3

Given the commit hash of a fix,how could I get the figure out how exactly the code is fixed?

How do I fix comment showing on code in javascript?

Cannot identify Error reasoning - Related to ArrayList and FOR loops, how could I fix my code?

How could I fix my js code in order it works with Bootstrap 4 collapse-div?

Why does my code keep get a comparison failure at line 2, and how could I fix this?

how could I fix this mistake of routes in react

How could I padding spaces to a fix length

How could I fix the animation speed?

How could I fix the lines being read?

How could could I optimize this PHP code?

How to fix recursion function

How to fix recursion in php?

How to handle recursion in FIX?

How can I skip a piece of code in recursion?

I want my code to repeat 3 times but the loop does not end. Does anyone know how I could fix this issue?

How do I fix a "too much recursion" error in ReactJS?

How do I replace for loops with recursion in Javascript?

How do I fix "no match for call to" in recursion inside a function? (in a permutation recursion algorithm)

How do I fix this calculator UI? (Javascript from code.org) I will explain down below

I'm working with Javascript, but VS Code gives me typescript intellisense. How do I fix it?

Simple web scraper formatting, how could I fix this?

How should i fix required a bean of type that could not be found?

How could I safely fix my walking "dead pixel" bug?

How can I fix this error? "Method 'str' could not be resolved"

How do I fix CURL error "Could not resolve host"?