TIL: For certain reasons π₯ I did a coding challenge on Codility for once and now I want to share my approaches with you.
Table of content...
For further actions, you may consider blocking this person and/or reporting abuse
Hello! I made an account just to ask this. What is happening in the filter line of the for loop solution? I have never seen a double implementation of the arrow function like that. What is it doing/yielding? Thank you!
Hey Michael. I am not quite sure if I understand your question right.
As described in Step 4: The for-loop checks if the number in the array is bigger then x, and when it is, then we already have the solution 1.
Apologies. I only referenced the for loop so you knew which solution I was referring to. The line of code I am asking about is the filter, which is where the double arrow function is being used.
Thank you :)
I still donβt understand to be honest π are you talking about this line of code, which is described in Step 1 and 2 of Approach 1: For loop?
const pos = A.filter(num => num >= 1).sort((a, b) => a - b);
I'm so sorry I'm not being clear enough! Yes, that is the line I'm referring to. I should have just copied and pasted it like you did. Right here I see two arrow functions being used back to back in a way I've never seen before, and do not understand what is happening there.
(num => num >= 1)
I see. :)
These are actually not two arrow functions. The first part
num =>
means like take each number in the list, the second partnum >= 1
is a comparison for each number in the list (like the action you want to do after you crabbed each number of the list) meaning for each num bigger or equal to 1 in this list.Hopefully, everything is clear now. Please reach out to me any time if you have further questions.
Ha! Wow, I feel silly. Glad I asked though. Thank you for being patient with me, I thought I was witnessing some new sorcery. That makes perfect sense now.
Oh please, donβt feel silly! We are here to learn and share π
Thank you for that. In the gaming community there is an acronym of RTFC, and I certainly failed to meet that standard here!
Nice solutions! The disadvantage of the map is that you need to wait the map() iterate on all itens in the array, whilst 1 and 3 you end the function as soon you find a valid value. Have you try to benchmark those approachs to see which one performs better?
Thanks for your your comment. Actually, I wanted to test all three of them on performance but I am still new to performance measuring. I am curious though. Hopefully I can add measurements to each solution soon.
I'd go with something like solution one but not bother to sort or filter the array first. Filtering alone is
O(n)
and you can easily get anO(n)
solution without needing to filter, let alone sort.Nice. Would you mind posting your solution here? I always try to make my coding tutorials as beginner friendly as possible, but it would be nice to add a more advanced solution as well!