JSFiddle - React, Tailwind, and code Playground

JavaScript

function myFunctions(n) {
        
  var list = [];
  for (var i = 0; i < n; i++) {    
    list.push(function(){
        return i;
    });    
  }
  return list;
}

var res = [];

myFunctions(4).map(function(el){
    res.push(el());
});

var p = document.createElement('p');
p.innerHTML = res.join(',');
document.body.appendChild(p);

//expected result: 0,1,2,3