Why won't my code work?

Hi, i’m doing the lodash challenge and I can’t figure out why my object function is not returning correctly in the test. Can someone please look at my code, thanks.

  findKey (obj , func) {
    for (let key in obj){
      let instance = func(obj[key]);
      if(instance){
        return key;
      }
      else {
        return undefined;
      }
    }
  }

Sorry a bit of context, the function is supposed to return the key where a value is present and not undefined.

You want to go through every key value pair in the object before you return undefined. Right now you’ll return undefined and exit the findKey function if the first instance is not truthy.

2 Likes

Thanks so much. I have one more question about that exercise. On the final section you have to create a method, chunk, that returns arrays of a designated size within the original array.

My question concerns the for loop. If I am incrementing by the size argument then I would have thought that I am skipping all the elements between i and that specified size? This code would imply that I am still registering all the elements in between but that doesn’t make sense if the increment is greater than 1? The correct code is as below:

  chunk (array , size=1) {
    let newArray = [];
    for (i = 0; i < array.length; i+= size){
      let arrayChunk = array.slice(i , i+size);
      newArray.push(arrayChunk)
    }
    return newArray
  }
};