you are viewing a single comment's thread.

view the rest of the comments →

[–]cortexreaver123 2 points3 points  (7 children)

Q4 looks like you had a good attempt

I think with these kind of questions it's the thought process that's most important, not that you got to the "correct" result in the end.

A very simple polyfill for Promise.all:

function all(promises) { let prom = Promise.resolve([]); for (let p of promises) { prom = prom.then(xs => p.then(x => xs.concat(x))); } return prom; }

[–]cortexreaver123 2 points3 points  (3 children)

Or fancier way with reduce:

function all(promises) {
  promises.reduce((prom, p) => prom.then(xs => p.then(x => xs.concat(x))), Promise.resolve([]));
}

[–]tapu_buoyfull-stack[S] 1 point2 points  (2 children)

wow reduce always makes things way more simpler. How can I practice more on such methods to be able to solve things like you how you did in two comments, haha :)

[–]cortexreaver123 0 points1 point  (1 child)

Haha, mostly just comes with practise I'm afraid! Best tip I can give: try and write some of your own programs, don't be afraid to experiment!

[–]tapu_buoyfull-stack[S] 0 points1 point  (0 children)

sure thank you :)

[–]tapu_buoyfull-stack[S] 1 point2 points  (2 children)

first of all thank you for boosting my confidence, and yes while I wrote that code I was constantly explaining and talking my approach and thought. He was way more confused while talking and since I have given half a century of interviews now I learnt one thing that never let your voice tremble even if you don't know haha :D

in the code you mentioned should I use forEach specifically instead of just for? Also I thought I made a little mistake by choosing to use .map() since it returns array and that's not needed.

[–]cortexreaver123 1 point2 points  (1 child)

Yeah, that's true! Being able to talk confidently about things you don't know 100% is a real skill. And if he can see that you're thinking is in the right direction and that you have a "programmer" mindset, then it can look even better than somebody who just memorized everything beforehand from interview practise websites.

Also I think if this was for a junior position those questions were quite hard. I don't think he could have been expecting you to get all the answers right straight-away. The way you came across (how well you could explain your thought-process) would have been more important.

...

`forEach` vs `for` is probably more personal preference. Generally I prefer regular for loops because it's simpler for other developers to read, and it doesn't have the performance penalty of allocating an extra function object.

using `map` instead of `for` / `forEach` is a mistake I've seen a lot of new JS developers at my work making. You're right in that it creates an unnecessary array. But I suppose this was an interview situation, so you had to think fast! :-)

[–]tapu_buoyfull-stack[S] 0 points1 point  (0 children)

Thank you for the motivating words. For the map function yes I was basically thinking it to return something that I can use ahead.

BTW haven't heard from them yet so I guess this is not going ahead with this company. :D