Question on Twitch API Challenge

Hi! Would greatly appreciate any help given as to why only online streams are showing up into the unordered list of id=“followerInfo” and the previous condition of (data.stream===null) is apparently being ignored.

You are trying to assign values to the variables before you know if there are values.
I’d add the variable assignment after the if statement.

Hmmmmm I moved it to after the ‘if’ and instead got 2 “unable to load” icons and “undefined” links. If I remove the 3 variables of displayName, logo and status, I instead get the 6 icons I assigned to be shown if the stream is offline, however the 2 online stream icons disappear… What gives?
My line of reasoning is this:
The loop goes through the array of channels and each loop does a get json/ajax.
Upon successful reception of json, 3 variables are taken which are the the display name, status and logo url of the stream. If the stream is offline these above 3 variables will return null which shouldn’t affect the ‘if’ statement which asks if the stream is offline/null. Right?
If it is offline/null, it will append an image to the unordered list which has been given an ID of ‘followerinfo’.
So what I should see is supposedly 2 online icon images since the first 2 streams in the array are online 24/7 and 6 identical offline images.
Yet this is not happening, leaving me very confused…

Well, you are not “taking” three variables.
You are taking the json response and are assigning part of it to variables.
When you are trying to access a property that doesn’t exist (when the channel is offline), you will throw an error “cannot read property, channel of null”. (see your browser console) and only the variables of the two online channels will have any values.

Where did you move the variables to?
They seem to be working in the else statement: https://codepen.io/c-herr/pen/ZKZXPv

Make a habit of checking your browser console. There are 2 big issues:

  1. if data.stream===null, then your assignment to logo is going to fail
  2. You need the var keyword in your loop:
for(var i=0;i<channels.length;i+=1) {
//...
}

OH! I see the light now! Muchos gracias!

1 Like