Amazon Web Developer Loop Timeout Interview Question
What to expect from this junior developer interview?
Rest, sleep, relax and sip on some tea. You will be fine! If you fail u will learn what to do next time. Dont stress and maybe do some light reading but dont kill urself
More on reddit.comJunior dev interview questions?
Consider the following code snippet:
for (var i = 0; i
(a) What gets logged to the console when the user clicks on “Button 4” and why?
(b) Provide one or more alternate implementations that will work as expected.
for (var i = 0; i < 5; i++) {
var btn = document.createElement('button');
btn.appendChild(document.createTextNode('Button ' + i));
btn.addEventListener('click', (function(i) {
return function() { console.log(i); };
})(i));
document.body.appendChild(btn);
}
Alternatively, you could wrap the entire call to btn.addEventListener in the new anonymous function:
for (var i = 0; i < 5; i++) {
var btn = document.createElement('button');
btn.appendChild(document.createTextNode('Button ' + i));
(function (i) {
btn.addEventListener('click', function() { console.log(i); });
})(i);
document.body.appendChild(btn);
}
Or, we could replace the for loop with a call to the array object’s native forEach method:
['a', 'b', 'c', 'd', 'e'].forEach(function (value, i) {
var btn = document.createElement('button');
btn.appendChild(document.createTextNode('Button ' + i));
btn.addEventListener('click', function() { console.log(i); });
document.body.appendChild(btn);
});
Lastly, the simplest solution, if you’re in an ES6/ES2015 context, is to use let i instead of var i:
for (let i = 0; i < 5; i++) {
var btn = document.createElement('button');
btn.appendChild(document.createTextNode('Button ' + i));
btn.addEventListener('click', function(){ console.log(i); });
document.body.appendChild(btn);
}
What is a potential pitfall with using typeof bar === "object" to determine if bar is an object? How can this pitfall be avoided?
var bar = null;
console.log(typeof bar === "object"); // logs true!
As long as one is aware of this, the problem can easily be avoided by also checking if bar is null:
console.log((bar !== null) && (typeof bar === "object")); // logs false
To be entirely thorough in our answer, there are two other things worth noting:
First, the above solution will return false if bar is a function. In most cases, this is the desired behavior, but in situations where you want to also return true for functions, you could amend the above solution to be:
console.log((bar !== null) && ((typeof bar === "object") || (typeof bar === "function")));
Second, the above solution will return true if bar is an array (e.g., if var bar = [];). In most cases, this is the desired behavior, since arrays are indeed objects, but in situations where you want to also false for arrays, you could amend the above solution to be:
console.log((bar !== null) && (typeof bar === "object") && (toString.call(bar) !== "[object Array]"));
However, there’s one other alternative that returns false for nulls, arrays, and functions, but true for objects:
console.log((bar !== null) && (bar.constructor === Object));
Or, if you’re using jQuery:
console.log((bar !== null) && (typeof bar === "object") && (! $.isArray(bar)));
ES5 makes the array case quite simple, including its own null check:
console.log(Array.isArray(bar));
What is the significance, and what are the benefits, of including 'use strict' at the beginning of a JavaScript source file?
Videos
Intro (feel free to skip) Hello. I am applying to a Web Developer position at Amazon and have made it through the phone screen with a recruiter and a technical phone interview using coderpad (a collaborative coding platform) with an Amazon engineer as well. During the technical interview, I was asked a question that I got wrong and I am still not sure what the solution is. (I was surprised to recently learn that I will be moving onto the onsite interview because I figured messing up on this question, which I perceive is considered easy, would be the end of my opportunity. But I guess my answers to the other questions, which, for anyone interested were about CSS Box Model, closures, hoisting, and DOM manipulation through JS, led to me passing on.) Any help on what the answer is would be much appreciated.
Interview Question
The interviewer asked me, "What is the output of this following code?":
const arr = [10, 12, 15, 21];
for (var i = 0; i < arr.length; i++) {
setTimeout(function() {
console.log('Index: ' + i + ', value: ' + arr[i]);
}, 3000);
}Even though I thought that was a trick question, I didn't have a better answer than
// Index: 0, value: 10 // Index: 1, value: 12 // Index: 2, value: 15 // Index: 3, value: 21
so that is what I put down as my response. The interview told me that that response was wrong and that the the actual output, after 3 seconds would be:
//Index: 4, value: undefined //Index: 4, value: undefined //Index: 4, value: undefined //Index: 4, value: undefined
He then asked me, "How can you manipulate the above code so that it does print out your answer?" Again, I was not sure (and obviously not really thinking judging my this upcoming answer that I gave), and so I just added arr and i as parameters to the timeout function so the for loop now read:
for (var i = 0; i < arr.length; i++) {
setTimeout(function(arr, i) {
console.log('Index: ' + i + ', value: ' + arr[i]);
}, 3000);
}I ran this in my console and saw that it also did not work. It just logged the following 4 times:
VM1718:4 Uncaught TypeError: Cannot read property 'undefined' of undefined
(Luckily, right as I wrote my answer in coderpad for the interviewer to see, he said that his browser tab crashed and that he had to reopen the tab and join back into the coding session. When he got back into the session with me after 10 seconds, for some reason, he just moved onto the next question. He seemed to have forgotten that he asked me another question about this timeout problem. Maybe his browser tab crashing saved my interview chances...)
My Question To You Anyone know how the for loop should be changed so that it logs each number and index? Also, what topic is this considered/ what should I read up on so I know more about the logic behind problem?
Thanks.
Edit: Grammar