# Twitch TV - Set Up

**URL:** <https://forum.freecodecamp.org/t/twitch-tv-set-up/31416>\
**Category:** General\
**Created:** [August 26, 2016, 4:05pm UTC](https://forum.freecodecamp.org/t/twitch-tv-set-up/31416 "2016-08-26T16:05:54Z")\
**Posts on this page:** 20\
**Page:** 1

<div class="post-metadata">

**Author:** ![Docwali777](https://sea3.discourse-cdn.com/freecodecamp/user_avatar/forum.freecodecamp.org/docwali777/32/65065_2.png) [@Docwali777](https://forum.freecodecamp.org/u/Docwali777)\
**Post date:** [August 26, 2016, 4:05pm UTC](https://forum.freecodecamp.org/t/twitch-tv-set-up/31416/1 "2016-08-26T16:05:54Z")

</div>

I need help the Twitch TV - ]

Having experience the previous projects with Random Quote Machine and Weather App. This project differs because it appears that I would need to display multiple “indices” from the array provided :

[“ESL\_SC2”, “OgamingSC2”, “cretetion”, “freecodecamp”, “storbeck”, “habathcx”, “RobotCaleb”, “noobs2ninjas”];

My approach initially was to iterate through the array - but I can create a function inside a loop. Here is my initial set up. I know how to evaluate each item in the array individually but I am stuck obtaining the information from the array simultaneously.

Suggestions would be appreciated…

` $(function() {  
var players = [“ESL\_SC2”, “OgamingSC2”, “cretetion”, “freecodecamp”, “storbeck”, “habathcx”, “RobotCaleb”, “noobs2ninjas”];

var users = [];  
var url = “[https://api.twitch.tv/kraken/streams](https://api.twitch.tv/kraken/streams)”;

$.ajax ({  
url: url,  
parameter:  
{  
client\_id: “9plca66vbfgmaj3narb9ebf3rxwafd0”,  
data: “json”  
},  
type: “application/javascipt”,  
method: “GET”,  
success: function(data) {

```
 console.log(data); 

}

```

});

}); `

---

<div class="post-metadata">

**Author:** ![jenovs](https://sea3.discourse-cdn.com/freecodecamp/user_avatar/forum.freecodecamp.org/jenovs/32/60963_2.png) [@jenovs](https://forum.freecodecamp.org/u/jenovs)\
**Post date:** [August 26, 2016, 4:30pm UTC](https://forum.freecodecamp.org/t/twitch-tv-set-up/31416/2 "2016-08-26T16:30:52Z")

</div>

> [@Docwali777](#):
>
> but I **can** create a function inside a loop

What do you mean by that? Did you mean ‘_can’t create_’? You absolutely can create a function inside a loop.

You loop through the array and make AJAX call for each array item and display received data (or save for later display).

> [@Docwali777](#):
>
> I am stuck obtaining the information from the array simultaneously

If you mean to get data for all of the array items at once, you can’t with js - js is single threaded.

Btw, what is `client_id` in your ajax call? I have suspicion you have misunderstood the assignment.

---

<div class="post-metadata">

**Author:** ![Docwali777](https://sea3.discourse-cdn.com/freecodecamp/user_avatar/forum.freecodecamp.org/docwali777/32/65065_2.png) [@Docwali777](https://forum.freecodecamp.org/u/Docwali777)\
**Post date:** [August 26, 2016, 5:07pm UTC](https://forum.freecodecamp.org/t/twitch-tv-set-up/31416/3 "2016-08-26T17:07:36Z")

</div>

correction -

as per CODE PEN - when I try to create the loop it states" I can not make a function inside a loop"…

when I was reading on git-hub it said to obtain a client\_id in order to use the API…

---

<div class="post-metadata">

**Author:** ![jenovs](https://sea3.discourse-cdn.com/freecodecamp/user_avatar/forum.freecodecamp.org/jenovs/32/60963_2.png) [@jenovs](https://forum.freecodecamp.org/u/jenovs)\
**Post date:** [August 26, 2016, 5:22pm UTC](https://forum.freecodecamp.org/t/twitch-tv-set-up/31416/4 "2016-08-26T17:22:08Z")

</div>

I pasted your code in codepen and can’t see any error.

> it said to obtain a client\_id in order to use the API

Looks like a lot of FCC Twitch TV projects will stop working over the coming months 😞

---

<div class="post-metadata">

**Author:** ![Docwali777](https://sea3.discourse-cdn.com/freecodecamp/user_avatar/forum.freecodecamp.org/docwali777/32/65065_2.png) [@Docwali777](https://forum.freecodecamp.org/u/Docwali777)\
**Post date:** [August 26, 2016, 5:24pm UTC](https://forum.freecodecamp.org/t/twitch-tv-set-up/31416/5 "2016-08-26T17:24:08Z")

</div>

// twitch client Id - 9plca66vbfgmaj3narb9ebf3rxwafd0  
//9plca66vbfgmaj3narb9ebf3rxwafd0

$(function() {  
var players = [“ESL\_SC2”, “OgamingSC2”, “cretetion”, “freecodecamp”, “storbeck”, “habathcx”, “RobotCaleb”, “noobs2ninjas”];  
var users = [];

for (var i = 0; i \< players.length; i++) {

var url = “[https://api.twitch.tv/kraken/streams/](https://api.twitch.tv/kraken/streams/)” + players[i] + “?callback=?”;

```
$.getJSON(url, function (json){
 console.log(json._links.self);
  
  
});

```

}  
});

---

<div class="post-metadata">

**Author:** ![jenovs](https://sea3.discourse-cdn.com/freecodecamp/user_avatar/forum.freecodecamp.org/jenovs/32/60963_2.png) [@jenovs](https://forum.freecodecamp.org/u/jenovs)\
**Post date:** [August 26, 2016, 5:30pm UTC](https://forum.freecodecamp.org/t/twitch-tv-set-up/31416/6 "2016-08-26T17:30:19Z")

</div>

Works for me.

Have you added jQuery in JS settings?

---

<div class="post-metadata">

**Author:** ![Docwali777](https://sea3.discourse-cdn.com/freecodecamp/user_avatar/forum.freecodecamp.org/docwali777/32/65065_2.png) [@Docwali777](https://forum.freecodecamp.org/u/Docwali777)\
**Post date:** [August 26, 2016, 5:31pm UTC](https://forum.freecodecamp.org/t/twitch-tv-set-up/31416/7 "2016-08-26T17:31:34Z")

</div>

> **[Twitch TV](https://codepen.io/docwali777/pen/jAjpXz?editors=1011)**
>
> ...

---

<div class="post-metadata">

**Author:** ![Docwali777](https://sea3.discourse-cdn.com/freecodecamp/user_avatar/forum.freecodecamp.org/docwali777/32/65065_2.png) [@Docwali777](https://forum.freecodecamp.org/u/Docwali777)\
**Post date:** [August 26, 2016, 5:34pm UTC](https://forum.freecodecamp.org/t/twitch-tv-set-up/31416/8 "2016-08-26T17:34:17Z")

</div>

Yes I have jQuery in JS settings

---

<div class="post-metadata">

**Author:** ![Docwali777](https://sea3.discourse-cdn.com/freecodecamp/user_avatar/forum.freecodecamp.org/docwali777/32/65065_2.png) [@Docwali777](https://forum.freecodecamp.org/u/Docwali777)\
**Post date:** [August 26, 2016, 5:35pm UTC](https://forum.freecodecamp.org/t/twitch-tv-set-up/31416/9 "2016-08-26T17:35:44Z")

</div>

what do you have displayed in your console.log? mine is blank

---

<div class="post-metadata">

**Author:** ![jenovs](https://sea3.discourse-cdn.com/freecodecamp/user_avatar/forum.freecodecamp.org/jenovs/32/60963_2.png) [@jenovs](https://forum.freecodecamp.org/u/jenovs)\
**Post date:** [August 26, 2016, 6:01pm UTC](https://forum.freecodecamp.org/t/twitch-tv-set-up/31416/10 "2016-08-26T18:01:31Z")

</div>

You have missing `>` in HTML line 17

---

<div class="post-metadata">

**Author:** ![Docwali777](https://sea3.discourse-cdn.com/freecodecamp/user_avatar/forum.freecodecamp.org/docwali777/32/65065_2.png) [@Docwali777](https://forum.freecodecamp.org/u/Docwali777)\
**Post date:** [August 26, 2016, 6:27pm UTC](https://forum.freecodecamp.org/t/twitch-tv-set-up/31416/11 "2016-08-26T18:27:19Z")

</div>

you are incredible… adding the missing “\>” solved my issues…

How did you find that???

---

<div class="post-metadata">

**Author:** ![jenovs](https://sea3.discourse-cdn.com/freecodecamp/user_avatar/forum.freecodecamp.org/jenovs/32/60963_2.png) [@jenovs](https://forum.freecodecamp.org/u/jenovs)\
**Post date:** [August 26, 2016, 6:42pm UTC](https://forum.freecodecamp.org/t/twitch-tv-set-up/31416/12 "2016-08-26T18:42:34Z")

</div>

It was tricky 😉

I was getting some strange error in console. Quick google search showed that it has something to do with inner workings of Codepen (it wasn’t typical js error). So I started to comment out pieces of your js code waiting for the error to disappear.

After the whole js code was commented out, I commented out the HTML code, and the error disappeared. After that it was obvious that the error is somewhere in the HTML code 😌

---

<div class="post-metadata">

**Author:** ![Docwali777](https://sea3.discourse-cdn.com/freecodecamp/user_avatar/forum.freecodecamp.org/docwali777/32/65065_2.png) [@Docwali777](https://forum.freecodecamp.org/u/Docwali777)\
**Post date:** [August 27, 2016, 2:03pm UTC](https://forum.freecodecamp.org/t/twitch-tv-set-up/31416/13 "2016-08-27T14:03:27Z")

</div>

I mad some progress since yesterday -

I was able to get - the names and logos and separate them…

Can you provide some direction with distinguising those “Online” and those “Offline”…

> **[Twitch TV](https://codepen.io/docwali777/pen/jAjpXz)**
>
> ...

---

<div class="post-metadata">

**Author:** ![jenovs](https://sea3.discourse-cdn.com/freecodecamp/user_avatar/forum.freecodecamp.org/jenovs/32/60963_2.png) [@jenovs](https://forum.freecodecamp.org/u/jenovs)\
**Post date:** [August 27, 2016, 2:23pm UTC](https://forum.freecodecamp.org/t/twitch-tv-set-up/31416/14 "2016-08-27T14:23:04Z")

</div>

These lines:

```auto
        if (json.stream) {
          $(".users").append("Offline");
        } else {
          $(".users").append("Online")
        }

```

are appending to elements with class `users` (every time!!!). That’s why you have nine times on the first element, eight times on the second etc.

You could do something like this:

```auto
        let stream = "offline"
        if (json.stream) {
          stream = "online"
        }

        $(".box").append("<br><a ... </a>" + "<div>" + stream + "</div>");

```

---

<div class="post-metadata">

**Author:** ![Docwali777](https://sea3.discourse-cdn.com/freecodecamp/user_avatar/forum.freecodecamp.org/docwali777/32/65065_2.png) [@Docwali777](https://forum.freecodecamp.org/u/Docwali777)\
**Post date:** [August 27, 2016, 8:15pm UTC](https://forum.freecodecamp.org/t/twitch-tv-set-up/31416/15 "2016-08-27T20:15:24Z")

</div>

Thanks for your help… I did not quite understand the ‘let statement’ despite some research…

I used my basic understanding to JS and jQuery to make some really raw coding… but despite that somehow its coming together…

I am almost to the finish line…

---

<div class="post-metadata">

**Author:** ![Docwali777](https://sea3.discourse-cdn.com/freecodecamp/user_avatar/forum.freecodecamp.org/docwali777/32/65065_2.png) [@Docwali777](https://forum.freecodecamp.org/u/Docwali777)\
**Post date:** [August 27, 2016, 8:15pm UTC](https://forum.freecodecamp.org/t/twitch-tv-set-up/31416/16 "2016-08-27T20:15:54Z")

</div>

[Updated Twitch](http://codepen.io/docwali777/pen/jAjpXz)

---

<div class="post-metadata">

**Author:** ![jenovs](https://sea3.discourse-cdn.com/freecodecamp/user_avatar/forum.freecodecamp.org/jenovs/32/60963_2.png) [@jenovs](https://forum.freecodecamp.org/u/jenovs)\
**Post date:** [August 27, 2016, 8:47pm UTC](https://forum.freecodecamp.org/t/twitch-tv-set-up/31416/17 "2016-08-27T20:47:33Z")

</div>

In latest js version ES6 `var` is replaced with `let` and `const` - for changeable variables and non-changeable constants respectively.

---

<div class="post-metadata">

**Author:** ![Docwali777](https://sea3.discourse-cdn.com/freecodecamp/user_avatar/forum.freecodecamp.org/docwali777/32/65065_2.png) [@Docwali777](https://forum.freecodecamp.org/u/Docwali777)\
**Post date:** [August 27, 2016, 11:56pm UTC](https://forum.freecodecamp.org/t/twitch-tv-set-up/31416/18 "2016-08-27T23:56:08Z")

</div>

cool… I will definitely read up on that… the world of programming new to mew as I have been learning basics of HTML, CSS and JS since 6/18/16… There is tons to learn.

You seem to be experienced- you are a great resource-- appreciated the guidance and leads…

I have one more piece to go for TWITCH TV :

- display the accounts which are no longer available…

---

<div class="post-metadata">

**Author:** ![jenovs](https://sea3.discourse-cdn.com/freecodecamp/user_avatar/forum.freecodecamp.org/jenovs/32/60963_2.png) [@jenovs](https://forum.freecodecamp.org/u/jenovs)\
**Post date:** [August 28, 2016, 3:58am UTC](https://forum.freecodecamp.org/t/twitch-tv-set-up/31416/19 "2016-08-28T03:58:35Z")

</div>

You can put the request to for a closed channel in your browser’s address bar and see what you get:

Request:  
`https://api.twitch.tv/kraken/streams/brunofin`  
Response JSON:

```auto
{
  "error": "Unprocessable Entity",
  "message": "Channel 'brunofin' is unavailable",
  "status": 422
}

```

So you need to check if response contains `status` key with value 422.

* * *

Also if you request some random string:  
`https://api.twitch.tv/kraken/streams/yhdnprd993`  
you get:

```auto
{
  "error": "Not Found",
  "message": "Channel 'yhdnprd993' does not exist",
  "status": 404
}

```

You can check for `status 404` also.

---

<div class="post-metadata">

**Author:** ![Docwali777](https://sea3.discourse-cdn.com/freecodecamp/user_avatar/forum.freecodecamp.org/docwali777/32/65065_2.png) [@Docwali777](https://forum.freecodecamp.org/u/Docwali777)\
**Post date:** [August 28, 2016, 4:00am UTC](https://forum.freecodecamp.org/t/twitch-tv-set-up/31416/20 "2016-08-28T04:00:58Z")

</div>

Thanks for reviewing… i was messing around with it… should be fixed now…

By the way… I am having issues obtaining the accounts that are “unavailable”… Can you provide some clues or guidance… I tried

`If (json.error) { console.log(json.messge); }`  
No results in the console… thanks

Reply

[Next page](https://forum.freecodecamp.org/t/twitch-tv-set-up/31416.md?page=2)
