-1

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 Answer 1

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

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.