This question was asked during the interview at work interview my answer was recorded as wrong.
Asked Question : What is the output of this code block ?
const arr = [10, 12, 15, 21];
for (var i = 0; i < arr.length; i++) {
setTimeout(function() {
console.log('Index: ' + i + ', element: ' + arr[i]);
}, 3000);
}
My answer is :
index 0 element 10
index 1 element 12
index 2 element 15
index 3 element 21
but it is not correct. What is the right answer ?
letinstead ofvar. Or, even better, useforEachinsteadThis question was asked- what is the question that was asked. We know what you are asking, but you haven't actually stated the question you answered wrong