News for hackers. Stuff that matters.
Re:Ask HN: How to prepare for a Front-end Developer interview? by danyim on Sunday August 14, 2016 @08:30PM (#12287956)
I'm also in the process of interviewing for a front-end engineer position, so thank you for replying and providing feedback. I'm assuming it has to be written this way because the function being pushed was referencing i, which was within the scope of the closure. Turning it into an IIFE breaks out of that. Thoughts?

  // Assuming pre-ES6
  function iterate(k) {
    var funcs = [];
    for(var i = 0; i < k; i++) {
      (function(num) {
        funcs.push(function() {
          return num;
        });
      })(i);
    }
    return funcs;
  }

  var itTest = iterate(4);
  console.log(itTest[0]()); // 0
  console.log(itTest[1]()); // 1
  console.log(itTest[2]()); // 2
  console.log(itTest[3]()); // 3
Re:Ask HN: How to prepare for a Front-end Developer interview? by mathgeek on Sunday August 14, 2016 @10:50PM (#12288360)
I'd probably ask you to do the printouts from within the function itself.

If you haven't already read them, I highly recommend the You Don't Know JS series of books. One of them covers this exact question.

Re:Ask HN: How to prepare for a Front-end Developer interview? by weaksauce on Monday August 15, 2016 @02:10AM (#12288957)
neat series of books. thanks for the tip

https://github.com/getify/You-Dont-Know-JS

Re:Ask HN: How to prepare for a Front-end Developer interview? by ehsanu1 on Monday August 15, 2016 @12:29AM (#12288638)
If you can figure that out during an interview, you're solid on the question. That's the gist of it, and knowing that you can use `let` to make it work is good too.
Re:Ask HN: How to prepare for a Front-end Developer interview? by ricardobeat on Monday August 15, 2016 @08:41AM (#12290021)
Yup, that's what I was thinking of. As others have mentioned, if you can show this, and explain why `let` solves it too, you're good to go.