Lodash project

Theres no lodash project forum so I hope its okay I can ask this question.

Why doesn’t this loop work? Seems to me it does it once and stopped, but I have a condition, use i++?

Hello @board1774168228. The reason this loop doesn’t work is because you have a return inside the loop. All code after a return but inside a function is not evaluated-including loops. This means the loop will reach the return once, return array, and exit the function (and therefore the loop).

I hope this helps!

I changed it to this but it still doesn’t work? Thanks btw!

1 Like

Could you post the full code, please-as in, the full function? Also, try making the return on a new line.

Done. Still error. This is the full code. The coded-out bit is the solution they have given.

thanks so much.

What does this return?

1 - _.drop() is defined - Passed!

2 - Returns an array - Failed: _.drop([“hi”, “bye”]) returned [object Undefined] instead of [object Array].

@board1774168228, Please describe your logic.

Working down the code; drop()'s purpose is to remove from the beginning of an array a certain no. of elements (equal to numberToRemove).

If numberToRemove is true;
let a counter (i) = 0;
As long as the counter is less than the number to remove;
remove the first element from the array using .shift;
increase the counter’s value by 1;
Keep doing this until the counter is no equal to NumberToRemove;
then exit the loop and return the array (.shift does permanently modify the array apparently);
if there is no numberToRemove (!numberToRemove), just remove one element form the array.

At this point I realised I needed to return the array for the else statement so I did that.

Error message:

1 - _.drop() is defined - Passed!

2 - Returns an array - Passed!

3 - Drops one element if no number is specified - Passed!

4 - Drops the specified number of elements from the beginning of an array - Failed: _.drop([“hi”, “okay”, “yes”, “bye”], 2) returned [ bye ] instead of [ bye, yes ].

The algorithm uses a series of left shifts as per the numberToRemove. How useful is that algorithm at this high a level?

I understand, probably rally bad but still it should work no?

while (n) {
    array.shift()
    n--
}
return array
1 Like

THE Following has been updated:
It works now but I don’t get why those 2 changes all of a sudden made it work? I had an external counter, which shouldve counted up and caused the while loop to work until the counter was >number to remove.

You, contrastingly, ditched the counter, and just decreased the numberTOremove. Both methods achieve the same, but why did yours work and not mine.

EDIT: I just console.logged all the values and found my mistake!!! I managed to fix it using my initial counter. Turns out it works if i=1; since I used i=0; I would always remove one more letter than needed. Cheers!

Your post from yesterday does not have a return statement, hence it returned undefined.

1 Like

I just posted this update so sorry about that - just caught you a minute too late.

““EDIT : I just console.logged all the values and found my mistake! I managed to fix it using my initial counter. Turns out it works if i=1; since I used i=0; I would always remove one more letter than needed. Cheers!””

1 Like

This topic was automatically closed 41 days after the last reply. New replies are no longer allowed.