Twitch API javascript

Twitch API javascript
0.0 0

#1

Again I feel stuck at javascript. The code looks fine and I cannot find any other error. Still the output isn’t displayed. The codepen link is: https://codepen.io/thenepaligamer/full/MGZbmG/… Thanks in advanced.


#2

Your code will not run, because you have syntax errors in your code. Use the dropdown located on the right side of the JavaScript console and select Analyze JS. It will report a number of syntax related issues (some minor and some major).

Two big errors are in the following else if blocks of code:

      else if(data.stream === undefined){
        game = "Account Closed";
        status: "Offline"; // this is not correct assignment syntax
      }
      else {
        game = data.stream.game;
        status: "Online";  // this is not correct assignment syntax
      };

The colon is not used to make an assignment to a variable.

Another issue is the following attempt at using the ternary operator:

status === "online" ? $("#display").prepend(html) : $("#display").append(html);

The ternary operator should not be used as a replacement for just any if/else statement. It is supposed to be used when you need to assign a value based on a true or false condition. For example, your use would have been better written as:

if (status === "online") {
  $("#display").prepend(html)
}
else {
  $("#display").append(html);
}

Typical use of a ternary operator is used to replace the following if statement:

var myValue;
if (status === "online") {
  myValue = "on";
}
else {
  myValue = "off"
}

Use of ternary operator for above statement:

var myValue = status === "online" ? "on" : "off";

Lastly, you should only have one of the following:

$(document).ready(function(){

Once you fix all the errors above, then the code will actually run and then you will see errors showing in the browser’s console (Ctrl+Shft+J in Chrome).


#3

Thanks for the previous help. But again I’ve encountered some problems

image
I don’t know why null is not defined. I added some js library but still with no prevail. I didn’t find any relevant searches on web and some of them suggested adding library but still doesn’t work.
image