I'm facing an issue with my code where it is not behaving as expected. Although I believe the code itself is correct, there seems to be a problem that I'm unable to identify. The goal of the code is to display numbers 10 through 0 with a 2-second interval between each number, i.e., 10-9-8... and so on. The current behavior is that it displays the numbers in ascending order instead of descending. I would prefer to use a for loop rather than seeking a workaround solution. Below you can find the snippet of my code:
function fadeOut(elem)
{
for( var i=10; i>0; i-- )
{
(function(index)
{
setTimeout(function()
{
console.log(index);
}, index * 2000);
})(i);
}
}