Why won't my twitch json api work

The logo is always the same when the channel is offline. What should I do?

Tricky issue.

The issue is that your l loop is calling AJAX functions. So, while those are completing, JS keeps running the loop. When the loop is finished, they all have the same value for channel, the last one. Put a console.log('channel', channel); and see for yourself.

In order to get the value to “stick”, you need to bind it to that scope. One way to do this is to pass it into your success function by adding the line to your AJAX call:

  $.ajax({
    dataType: 'jsonp',
    type: 'GET',
    url :  url,
    channel: channel,
    success : function(result){ 

around line 17. Now you have two channels, the one in the global scope and one local to the ajax call. Now you can refer to the local one with this.channel around line 42 when you define url2. This will use the correct value. I don’t know if this is the best answer, but it’s the one I came up with.

I would add a few other observations. I would avoid using the letter “l” as a variable - it’s too easy to confuse with the number 1 or a capital I (in some fonts, all the three are the same symbol. The standard looping variable is either “i” (then “j”, then “k”) or something meaningful.

Also, be sure to organize, indent your code properly. Build these good habits now. To get started, in codepen, if you select all your code, you can do shft-tab to auto indent. On the JS window, there is also a v symbol in the upper right that will do a fuller rearrangement of your code.