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.
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.
I don't think many entry level programmers will think about returning a function to implement say, but I can be wrong.
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? function say(x)
{
return function (y) { alert( capitalize(x) + " " + capitalize(y)); }
}
Where capitalize makes the first character uppercase.Thanks for the heads up.
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
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 !!"
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.