With the help of the code, an array is created using a loop, three functions are written to the array. The logic of the functions is simple, the counter value at the time of creation of the function is output to the console. On the surface, the code looks logical, but if you run this code without changes, the number 3 will be output to the console twice. Using the closure mechanism, modify the code to output the number 0 and the number 2 to the console. It is important for me to write in ES5 and use the closure mechanism
var arr = [];
for (var i = 0; i <= 2; i++) {
arr[i] = function () {
console.log(i);
};
}
arr[0](); // 0
arr[arr.length - 1](); // 2