Simple question on return - lodash exercise - don't get what happens if you place a return here?

This is probably really silly, but in the code if you put return numberToRemove = 1 after the if statement, the code won’t run properly. I am just wondering, w hy would this be the case? Surely if you return it, since it is inside the if statement, the computer should move onto the next line outside the if statement, but now with numberToRemove = 1 (reassingment).

Not silly at all. So the question is how will it not run properly… when you call the drop() function: is it throwing an error, is it dropping another number, is it not returning the array altogether?

This is the error message using incorrect code:

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

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

Correct code for comparison:

Thanks a lot btw!

I might’ve mentioned in another post but it’s really useful to put tracers of all your values when you don’t know where to start digging.

Particularly, you should look into what your newArray is.
Double check what slice does too.
Whenever you have arrays, don’t forget about 0-indexing :sweat_smile:
Try putting in other values, 1 might not be working, but if you put in other values with your tracers, something might click in your head as to why 1 didn’t work.

It’s ok if you don’t get it now! The point is to push yourself to ask the questions, and position the answers to appear for you. Your brain might actually solve it by itself before your next study session…

1 Like

Its 3am in the UK haha, is it ok if i can try it tomorrow evening and report back to you? I think my brain is fried, since that lodash task had 43 exercises and took my ages to get through haha

As always thanks so much for taking the time to respond and help me :slight_smile:

Edit: Ok so I quickly checked everything and it all logs fine, I think the issue is the 'return" part, if I put return numberToRemove it goes wrong?

Yea you definitely don’t want to return numbertoremove because that’s not what you want your function to do (it should return an array, not a number).

And rest is vital for life and also learning haha, take a break.


1 Like

thanks - why exactly is it so bad to return a number tho, because numbertoremove can still be used in the let newArray = array.slice(numberToRemove) to then return a new array. Any ideas? I just don’t get it.

@mtf any ideas either if you don’t mind me asking?

well, remember you only get to return once in a function (since a return ends the function).

If the goal of your function is to take a specific number out, it implies that you want the remainder minus that specific number (hence, returning the array and not the numberToRemove).

You also already have the numberToRemove to begin with since that’s what you input into the parameters.

It’s ok to return a number in general! Just not in this function. If you wanted the function to do something else, then it would have a different name too. The drop name implies that you are dropping x num from y array.

1 Like

AHHH that makes so much sense!! So you can only return once, I didn’t know that! I thought it was once per statement…and since they were on different lines I thought it would only end the if statement not the entire function. That makes so much more sense.

Thanks man :slight_smile: