JSFiddle - React, Tailwind, and code Playground

by jameswestgate

JavaScript

//Example 2
function buildAlerter(list) {
    var result = [];
  
    for (var i = 0; i < list.length; i++) {
        
        (function(k) {
        
            result.push(function() {
                alert('This is item no. ' + list[k]);
            });
        })(i);
        
        
    }
    
    return result;
}

var list = buildAlerter([1,2,3]);
 
for (var j = 0; j < list.length; j++) {
    list[j]();
}