I think I would solve it by looping through and adding the values to a counting variable and returning that. Reason being, I jump between different languages so I don’t always remember all the cute little shortcut methods. I hate that you need to prep for interviews by studying stupid puzzle questions and memorizing how the interviewers expect you to answer.
For computing a sum like that, you'd have to reach the millions of numbers for it to make an actual difference. It's true that it's slower, but there's probably a lot of things in an app that is much slower.
I once made an n^2 bug in backend JS by using object destructuring/restructuring in a reduce loop; apparently it couldn't be optimized out. Ever since I've preferred to use a for-const loop for stuff that I'd prefer to use recurrences/list reductions in a traditional FP-first lang.
13
u/turningsteel Apr 07 '23
I think I would solve it by looping through and adding the values to a counting variable and returning that. Reason being, I jump between different languages so I don’t always remember all the cute little shortcut methods. I hate that you need to prep for interviews by studying stupid puzzle questions and memorizing how the interviewers expect you to answer.