Comments on: JavaScript interview questions https://j11y.io/general/javascript-interview-questions/ Sun, 22 Mar 2015 15:39:22 +0000 hourly 1 https://wordpress.org/?v=5.0.13 By: Robert https://j11y.io/general/javascript-interview-questions/#comment-2117 Sun, 20 May 2012 20:40:32 +0000 https://j11y.io/?p=1787#comment-2117 I noticed most folks used the map function for the string manipulation, whereas I went for array methods like Christian L for the browsers from Redmond.

var str = 'The quick brown fox jumps over the lazy dog'
str = str.split(' ');
for (var i = 0, len = str.length; i < len; i++) {
   str[i] = str[i] + (i + 1);
   str.join(' ');
}

No need to declare the length outside the loop. Having it declared inside still only gets executed once, like var i = 0. Also it’s more efficient to avoid string concatenation just to add spaces to the array indices. Passing a space as the argument for join accomplishes the same thing more efficiently.

]]>
By: CheckThisResume.com https://j11y.io/general/javascript-interview-questions/#comment-2116 Sun, 25 Mar 2012 00:13:38 +0000 https://j11y.io/?p=1787#comment-2116 //Replace the string “The quick brown fox jumps over the lazy dog”
//with the string “The1 quick2 brown3 fox4 jumps5 over6 the7 lazy8 dog9″
str=”The quick brown fox jumps over the lazy dog”;
str2=””;
str.split(‘ ‘).forEach(function(x){str2+=x+x.length+’ ‘});
alert(str2);

]]>
By: joe https://j11y.io/general/javascript-interview-questions/#comment-2115 Tue, 07 Feb 2012 21:05:32 +0000 https://j11y.io/?p=1787#comment-2115 “The quick brown fox”split(” “).map(function(e){
return e+(++i);
});

]]>
By: Christian L https://j11y.io/general/javascript-interview-questions/#comment-2114 Sun, 20 Nov 2011 21:24:08 +0000 https://j11y.io/?p=1787#comment-2114 ***Replace the string “The quick brown fox jumps over the lazy dog” with the string “The1 quick2 brown3 fox4 jumps5 over6 the7 lazy8 dog9”.***

var str = "The quick brown fox jumps over the lazy dog";
var str2 = str.split(" ");
 
var len = str2.length;
    for (var i = 0; i < len; i++){
     str2[i] +=  (1 + i) + " "
    }
 
str2 = str2.join("");
]]>
By: Alexander https://j11y.io/general/javascript-interview-questions/#comment-2113 Sun, 11 Sep 2011 19:28:01 +0000 https://j11y.io/?p=1787#comment-2113 Fair, fair

now i know that i’m not a javascript developer… yet :p

]]>
By: Gareth Elms https://j11y.io/general/javascript-interview-questions/#comment-2112 Sun, 07 Aug 2011 16:15:27 +0000 https://j11y.io/?p=1787#comment-2112 Really enjoyed going through these. More…!

]]>
By: alpha123 https://j11y.io/general/javascript-interview-questions/#comment-2111 Thu, 28 Jul 2011 19:09:55 +0000 https://j11y.io/?p=1787#comment-2111 If I was interviewing someone for a JavaScript position, I’d probably just have them write a basic CSS selector engine. Difficult, but fun. I’ve written 3. 😛 Usually doesn’t take more than a day or two to have something working well.

Oh, and here’s my slightly hackish #9:

'The quick brown fox jumped over the lazy dog'.split(' ').reduce(function (p, c, i) { return (!+p ? p + ' ' : '') + c + (i + 1) }, 1)
]]>
By: GDmac https://j11y.io/general/javascript-interview-questions/#comment-2110 Sun, 24 Jul 2011 13:51:31 +0000 https://j11y.io/?p=1787#comment-2110 I like the PHP explanation of the comparison types (i.e. equal vs. identical)
( http://www.php.net/manual/en/language.operators.comparison.php )

"1" == 1; // Equal
"1" === 1; // Identical
]]>
By: Sergio Cinos https://j11y.io/general/javascript-interview-questions/#comment-2109 Sun, 17 Jul 2011 04:41:53 +0000 https://j11y.io/?p=1787#comment-2109 Great questions! I usually interview JS engineers at my current job, so probably I’ll use some of them :). Here are my thoughts about some questions:

#2 – Good one, most people miss undefined and null.

#3 – Good one. I’d add the usual closure created inside a loop and trying to use the loop index.

#4 – Good. Using eval is a total fail for the candidate.

#5 – Ok. Not using an anonymous function is a total fail.

#6 – I think Array.prototype.reduce is a better choice. You have to deal with empty initialValue, that is very tricky. If the candidate’s implementation doesn’t match the Spec completely (specially for corner cases), it’s ok, but I’ll show the spec to the candidate and ask for a full implementation (let’s check if the candidate can read and implement a spec). BTW, all solutions provided in previous comments lack type checking (double fail: it is in the spec and omit type checking is a very bad practice).

#11 – Just replace &. Changing %22 is a bad (you are mixing url encoding and html encoding).

#12 – Not JavaScript, I don’t care if a JS Engineer doesn’t know about it.

#13 – I don’t care if a JS Engineer doesn’t know about how to implement something in jQuery. I expect him to be able to discuss about jQuery good features vs. bad features, and build his own framework/library.

]]>
By: Garrett https://j11y.io/general/javascript-interview-questions/#comment-2108 Sun, 10 Jul 2011 18:24:40 +0000 https://j11y.io/?p=1787#comment-2108 Surprised seeing so many getting the answer to question 1 wrong! And when I see a question whose answer is “implementation dependent” or “nonstandard”, I worry that the interviewer might have the wrong answer.

So here’s the deal with parseInt.

ECMAScript 3 discourages octal interpretation but allows it; so the answer is “implementation dependent — don’t expect a consistent result from parseInt(‘010’);”

When radix is 0 or undefined and the string’s number begins with a 0 digit not followed by an x or X, then the implementation may, at its discretion, interpret the number either as being octal or as being decimal. Implementations are encouraged to interpret numbers in this case as being decimal.

Says that octal is optional.

ES5 banned the octal interpretation allowance mistake. The function parseInt no longer allows implementations to treat Strings beginning with a 0 character as octal values:

The parseInt function produces an integer value dictated by interpretation of the contents of the string argument according to the specified radix. Leading white space in string is ignored. If radix is undefined or 0, it is assumed to be 10 except when the number begins with the character pairs 0x or 0X, in which case a radix of 16 is assumed. If radix is 16, number may also optionally begin with the character pairs 0x or 0X.

But web browsers c2011 are not caught up to the new spec — Firefox 5 and Safari 5 parse octal when no radix is specified, in violation of ECMAScript 5.

parseInt("010"); // A result of '8' is a violation of ES5

I’ve updated MDC docs for parseInt regarding octal interpretation in ES3 and how it is forbidden in ES5.
https://developer.mozilla.org/en/JavaScript/Reference/Global_Objects/parseInt

]]>