Hacker News new | ask | show | jobs
by Xavi 5622 days ago
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.

2 comments

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?
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

Not exactly the same signature, but can handle arbitrary number of calls.

var say = (function() {

    var savedArgs = [];

    return (function recurse () {

        if (arguments.length) {

            for (var i=0, arg; arg=arguments[i]; i++) {

                savedArgs.push(arg);

            }

            return recurse;

        } else {

            alert(savedArgs.join(' '));

            savedArgs = [];

        }

    });
})();

// e.g. say('Hello')('World')('!!')();

// --> alerts "Hello World !!"

here's the version I came up with, based on one I did in python. It's very similar to jaysoo's - needs a call with no args to terminate the list. I'm curious if there's a way to do this somehow without the terminator.

I'm not a JS expert by any means, so if I'm doing something stupid in this code, I'd love to know.

  var say = function(word) {
    
    words = [word];

    func = function(word2) {
        if (word2) {
            words.push(word2);
            return func;
        }
        else {
            print(words.join(' '));
        }
    }

    return func;
  };
say('hello')('bob')('how you doing?')();

*note I have the function 'print' aliased to console.log for convenience.

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.

They seem like good questions. As an addendum to the final question in second link, it might also be worthwhile checking the interviewee's understanding of "this" in other contexts too.