Twitch TV help, only displaying 1 person

I am doing the Freecodecamp Twitch TV project where I show if 9 streamers are online. I use this array to store the streamers:


var streamers = [

I use these two API’s, I used a for loop which I explained later.

Channel/streamer details:


Stream details (to check if their streaming)


I used this code to access these API’s

for (var i = 0; i < streamers.length; i++) {
    var streamStatusApi =
      "" +
      streamers[i] +

    var channelApi = "" +
      streamers[i] +

    $.getJSON(streamStatusApi, function(data) {
      $.getJSON(channelApi, function(data2) {
       if( == null) {
        // here i pushed something into the html based on stream status

       else if ( == true) {
          // here i pushed something  into based on stream status.
    }); // $.getJSON for stream status
  } // for loop

So why is only noobs2ninjas’s stream status being displayed on the html? Basically the last name in the array. Someone suggested that its because the two JSON’s are inside each other. I need information from both API’s. Would the variables still be global if they were separated (and made outside the JSON)?

The problem is because you declared the streamStatusApi and channelApi variables with the var keyword. Such variables remain alive even after the for-loop (or block in general) they are in finished executing. It’s true that the outer .getJSON calls use the correct URLs, but the inner one uses the last value that the channelApi variable takes (which is the URL for noobs2ninjas).

You can peek at the values of these variables by “tapping” with a console.log:

$.getJSON((console.log({streamStatusApi}), streamStatusApi), function(data) {
  $.getJSON((console.log({channelApi}), channelApi), function(data2) {

Declaring these variables with let makes them block-scoped, and they are alive only for each iteration of the for loop.

In addition to what @kevcomedia said, your else if block of code will never execute, because stream is either going to be an object or the value null. Your code checks if stream is equal to true. You do not even need an else if, you just need an else.

else  if ( == true) { // never going to be true

Thanks guys, but how to lock my image in place (offline/ online)?