Can't get twitch tv stream to show online status

I’m just about done with the project but I can’t get the page to display whether the channel is online or not. I think the problem may have to do with the getjson call in the middle of the other getjson call, but I don’t know any other way to do it. The stream information doesn’t appear to be in the first json call so I need to make another one, but when I do using the same strategy as the first, all the channels come back as undefined with null status on the stream. Thanks. Here is my javascript code. Sorry if the code is a little hard to read, codepen often formats the code oddly and I have trouble counteracting it.

$(document).ready(function() {
   
   

var url2 = " https://wind-bow.glitch.me/twitch-api/channels/";
var url3= "https://wind-bow.glitch.me/twitch-api/streams/";
var userNames = ["ESL_SC2", "OgamingSC2", "cretetion",  "storbeck", "habathcx", "RobotCaleb", "noobs2ninjas","Freecodecamp"]
   for(var i = 0; i<userNames.length;i++){
           $.getJSON(url2+userNames[i],function(data){
         
         
            var onlineStatus;
            $.getJSON(url3+userNames[i],function(data2){
               if (data2.stream === null){onlineStatus="OFFLINE"}
               else{onlineStatus = "ONLINE"}
                 $('#channelInfo').prepend("<br><img src='"+data.logo+"' class = 'left' style=     'width:120px; border-radius:100%; bottom:100px;' > <br>");
       
                 $('#channelInfo').prepend("<ul class='center' style='font-size:50px;'>"+ "<a href ='"+data.url+"'>" + data.display_name +" </a>"+onlineStatus+ "</ul>");
         
                 if(data.status !== null){ $('#channelInfo').prepend("<ul class='right' style='font-size:20px;'>" + data.status +"</ul>" );
                                 }
                  $('#channelInfo').prepend("<hr>")
       
             });


         
      });
 }
});

And here is the link to the codepen

Thanks Randelldawson! That simple change of var to let really fixed the problem. Interesting, I never would have expected such a simple change would have fixed the problem. I will definitely read more about the difference of let to var.

It is definitely interesting seeing a situation where (var) screws up code while (let) allows it to run. I had another question. I have noticed that the links to the channels only work when I right-click (“open in new tab”). Is there some way to fix this? I have noticed the project that freecodecamp gave as an example has a similar problem as well. At least that is what is happening in my browser when I run their codepen.

Thanks again! I really appreciate it

Wow, definitely a bit more dense than what I had originally made. Do IIFE still have any value of using these days?