DEV Community

What is faster then a FOR..Loop in JS?

Heriberto Garcia Ribas on August 01, 2019

Hello everyone, I hope you are doing good! I am not sure if this is the right way to ask a question in here? Forgive me in that case. I just joine...
Collapse
 
montanacoder profile image
Jeremy • Edited

This one was tricky for me too. you may be overthinking this. To answer you header question, while loops and recursion are loops that will be faster than a for loop if I am not mistaken. As for the problem, without giving you the answer, (I am bad with hints) try reading the question like this, " how many numbers does "n" have odd and even" (maybe that will help, i know it triggered my brain)? meaning, if we split the number in half we will get 12 even and 1/2 odd (not fully but close) so if we take the number 3 for easy math (haha) 3 = 1 even (2) and 1 odd (1),
do the same thing with 7, 7= 3 even (6,4,2) and 3odd(5,3,1) and 15 = 7even (14,12,10,8,6,4,2) and 7odd (13,11,9,7,5,3,1) but if we take the number "n" and divide it in half we will end up .5 at the end ie (n=15 => 7.5) from there you will want to look at Math Method to achieve the correct answer. I hope I helped and didn't confuse you. your final answer should be one line

developer.mozilla.org/en-US/docs/W...

Collapse
 
hgarr profile image
Heriberto Garcia Ribas
hgarr image

OMG of course! I was overthinking it indeed.
There is no need for a loop.

Math.floor(n/2)

From now on I will take on the Kata's with a different perspective!
Thanks Jeremy! :)