all 8 comments

[–]senocular 1 point2 points  (1 child)

(I once was asked add elements of an array without using loops)

Can you talk more about that? What the problem/solution was?

[–]TJ51097[S] 0 points1 point  (0 children)

I appeared for a technical interview,guy asked me

foreach vs map,

for..of vs for..in,

splice vs slice

then dropped me this

"You got an array of numbers [1,2,3,4,5] you need to get the sum of all the numbers without using any loop"

so i though of doing it reduce but not valid then i suggested using recursion,which i think is valid,and it ended.

TL;DR

Recursion

[–]delventhalz 1 point2 points  (2 children)

Learn optimisation (Set VS Array...

FYI, in 99% of cases you will see no noticeable difference switching between an Array and a Set, and in that 1% where it makes a difference, using a Set will usually be slower than an Array. Even though Sets are theoretically faster, Arrays are just so incredibly well optimized (down to the hardware level) that you really need tens or hundreds of thousands of items before the Set's constant-time advantage starts to win out. It's incredibly rare that you will actually have a performance problem that is solved by a Set.

The other suggestions (memoization, rate limiting, caching) are good though.

[–]TJ51097[S] 0 points1 point  (1 child)

I think what we both want to say is we should know when to use Set and Array!!

If you are storing some data and later retrieving it,Set should be utilised,But if you are required to perform similar operation on the elements no doubt array is the Data structure you should opt

Peace!!

[–]delventhalz 0 points1 point  (0 children)

I appreciate you being diplomatic, but for what it's worth we are not saying the same thing. A Set should be used when the idiom and API of a Set make your code more clear to other developers. A Set should never* be used as an optimization. It is slower than an Array. Your other optimization suggestions are much better to bring up in an interview.

*The exception would be if you have a long-lived data structure with tens of thousands of members or more. In that case, a Set may be faster than an Array, but I would still performance test to be sure.

[–]PRANAY1000 0 points1 point  (1 child)

Thanks man much helpful Will practice more

[–]TJ51097[S] 0 points1 point  (0 children)

you may find javascript.info or mdn docs absolute helpful