Live data from Hacker News

JavaScript Interview Question

cam.ly

51–60 of 63 posts

Re: JavaScript Interview Question

#51
post #28

Earlier quoted context omitted.

this is what I usually tell people to do: var els = document.getElementsByTagName('li');//use var unless you really really really meant global for(var i=0; i

Does this work (from a non-JS person -- I like reading JS books, but don't code in it at all). This is certainly less elegant, but seems easier to follow: var els = document.getElementsByTagName('li'); for(i=0; i

No, it doesn't because j is in the same scope as i.

Your confusion is because of a javascript quirk

  for (...) {
    var foo = bar
  }
is actually the same as

  var foo
  for (...) {
    foo = bar
  }
Notice how you only ever have one foo

What you need is:

  for (...) {
    (function(bar) {
       var foo = bar
    })(bar)
  }
which create a new foo every time you loop

Re: JavaScript Interview Question

#52
post #3

A while back a couple of colleagues sat down and came up with our own set of JS interview questions. Here's what we came up with: Can the interviewee code: https://gist.github.com/633341 Can the interviewee read/debug code: https://gist.github.com/633087 I used these questions during a couple of interviews. They seemed to work well.

What its the percentage of developers that pass the questions? I don't think many entry level programmers will think about returning a function to implement say, but I can be wrong.

Many interviewees, when presented with `say("hello")("world")`, understood that `say` was a function that returned a function, but few were able to actually implement `say`.

Re: JavaScript Interview Question

#54
post #52

Earlier quoted context omitted.

What its the percentage of developers that pass the questions? I don't think many entry level programmers will think about returning a function to implement say, but I can be wrong.

Many interviewees, when presented with `say("hello")("world")`, understood that `say` was a function that returned a function, but few were able to actually implement `say`.

I like this question, and I've got something that works, but I'm not sure it's the prefered solution.

  function say(arg) {
    return function(word) {
      alert(arg + " " + word);
    };
  }
It solves "hello world", but wouldn't work for something like say("dogs")("and")("cats"). Are you expecting an arbitrary number of function returns?

Re: JavaScript Interview Question

#55
post #43

Earlier quoted context omitted.

i agree with the choice of code, but I have a different style for laying out the code. i dislike the declaration and immediate execution of an anonymous function. i much prefer to give it a name, move it out of the loop itself and then call it within the loop. it makes reading the code more top-to-bottom. with this approach, when I get to the anon func declaration, i have to scan down looking for when it is invoked.…

I don't generally care too much to talk about style when the problem at hand is that someone has been banging their head against the desk for the past two hours and they just want it to work (and, in my experience, this usually is the case, for some reason) But yeah, if we're gonna be super nitpicky, yeah sure, improve the for loop also, while we're at it. for (var i = -1; els[++i];) {//given the code in the loop bod…

Wouldn't var i have to be set to -1:

  for (var i = -1; els[++i];)  ?
Otherwise, the alert won't trigger on the first li element.

Re: JavaScript Interview Question

#56
post #55
post #43

Earlier quoted context omitted.

I don't generally care too much to talk about style when the problem at hand is that someone has been banging their head against the desk for the past two hours and they just want it to work (and, in my experience, this usually is the case, for some reason) But yeah, if we're gonna be super nitpicky, yeah sure, improve the for loop also, while we're at it. for (var i = -1; els[++i];) {//given the code in the loop bod…

Wouldn't var i have to be set to -1: for (var i = -1; els[++i];) ? Otherwise, the alert won't trigger on the first li element.

Hah, good catch. Was thinking of i++ for some reason. Fixed.

Re: JavaScript Interview Question

#57
post #52

Earlier quoted context omitted.

Many interviewees, when presented with `say("hello")("world")`, understood that `say` was a function that returned a function, but few were able to actually implement `say`.

I like this question, and I've got something that works, but I'm not sure it's the prefered solution. function say(arg) { return function(word) { alert(arg + " " + word); }; } It solves "hello world", but wouldn't work for something like say("dogs")("and")("cats"). Are you expecting an arbitrary number of function returns?

According to the expected output, you'd need something like:

    function say(x)
    {
    	return function (y) { alert( capitalize(x) + " " + capitalize(y)); }
    }
Where capitalize makes the first character uppercase.

Re: JavaScript Interview Question

#58
post #52

Earlier quoted context omitted.

Many interviewees, when presented with `say("hello")("world")`, understood that `say` was a function that returned a function, but few were able to actually implement `say`.

I like this question, and I've got something that works, but I'm not sure it's the prefered solution. function say(arg) { return function(word) { alert(arg + " " + word); }; } It solves "hello world", but wouldn't work for something like say("dogs")("and")("cats"). Are you expecting an arbitrary number of function returns?

Yep, that is exactly the solution I would be looking for.

I would be blown a way if someone came up with solution that could handle arbitrary function returns. My friend actually wrote an interesting article on the subject. Here's the link if you're interested: http://msdn.microsoft.com/en-US/scriptjunkie/gg575560.aspx

Re: JavaScript Interview Question

#59

Earlier quoted context omitted.

I like this question, and I've got something that works, but I'm not sure it's the prefered solution. function say(arg) { return function(word) { alert(arg + " " + word); }; } It solves "hello world", but wouldn't work for something like say("dogs")("and")("cats"). Are you expecting an arbitrary number of function returns?

According to the expected output, you'd need something like: function say(x) { return function (y) { alert( capitalize(x) + " " + capitalize(y)); } } Where capitalize makes the first character uppercase.

Oops, that's a type-o. The capitalize doesn't need to change.

Thanks for the heads up.

Post reply on HN