Twitch app JS I NEED HELP[SOLVED]

$(document).ready(function() {
var user = [“ESL_SC2”, “OgamingSC2”, “cretetion”, “freecodecamp”, “storbeck”, “habathcx”, “RobotCaleb”, “noobs2ninjas”]
var logo = [];
var status = [];
var name = [];

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

var streamsURL = "https://api.twitch.tv/kraken/streams/" + user[i];
var userURL = "https://api.twitch.tv/kraken/users/" + user[i];
$.getJSON(userURL, function(userInfo) {

    logo.push(userInfo.logo);
    status.push(userInfo.status);
    name.push(userInfo.display_name);
    
    console.log(name)

    $("#followerInfo").prepend("<div class='row'>" + "<div class='col-md-4'>" +
        "<img src='logo'" + "'>" + "</div>" +
        "<div class='col-md-4'>" + name +
        "</div>" + "<div class='col-md-4'>" + status + "</div>" +
        "</div>");

});

$.getJSON(streamsURL, function(data4) {


});

}

});

With this call I get a array on display names but I can’t seem to get them one by one, any suggestions?

Can you clarify what you mean by ‘a array on display names’? what does the result look like?

$.getJSON or any other AJAX call will often return an object in JSON format for you to extract values from. In fact, you’ve already done this in these lines, where each userinfo._____ is accessing a property of the object you’ve just got back from the ajax call.

logo.push(userInfo.logo);
status.push(userInfo.status);
name.push(userInfo.display_name);

Can you give us an output of the array that you’re getting back.

Perhaps run console.log(userInfo); and post what data you’re getting back?

Can you post your CodePen?

http://codepen.io/Mr-Fortune420/pen/AXgvmb?editors=0010

Here you are. Yes, I now understand that this is sending me a whole array of display names, however, I have tried everything from split to reduce to reduceRight and I’ve tried to display name[i] but nothing has worked.

Thanks guys!

This is what happens if I console.log(userInfo)

You’re pushing data to an array and then displaying the entire array instead of individual names. I’ve forked your pen with changes that make it work:

http://codepen.io/PortableStick/pen/vKzRRB?editors=0010

Check out the comments inside and let me know if you have any other questions!

Thanks a lot @PortableStick that was exact. You’ve successfully launched me into the next hundred hours of torturous algorithms I am to endure!

1 Like

Just be sure you understand why it works now, rather than just copying the code from @PortableStick - I understand he was trying to be helpful, but just changing the code for you, IMO is not a good way to learn and / or teach.

Your concern is duly noted, but the problem and solution were both very simple. The working example is annotated, and the why is obvious.

@PortableStick it may have been simple but it was a drastic change ya?! @geligelu Thanks for the concern. I would like to think I’m smarter than a copy and paste type of person. That being said, here I go again asking for help.

function sumAll(arr) {
var newArr = [];
var big = Math.max(arr[0],arr[1]);
var small = Math.min(arr[0],arr[1]);

for (var i = small; i<arr.length;i++){
if(i < big){
arr.push(i);

}
}
for (var j = big; i<arr.length;i++){
if(j > small){
arr.push(j);

}
}

for (var n = small; n<arr.length; n++){
if(n !== arr[i]) {
newArr.push(n);
}
}
return newArr.reduce(function(a, b){
return a+b;
}
);

}

sumAll([1, 4]);

So, with this function, I am getting what I am supposed to with 1, 4. and 4, 1. When I try to plug in 5, 10, and 10, 5, though, it doesnt even run the for loop.

In other words I don’t think it’s running the for loop unless “small” is one. If it is bigger it won’t seem to compute!

Yup! I like the progress you made on the Twitch Viewer. As for the algorithm (I assume this is for Sum All Numbers in a Range), you’re very close. In fact, you’ve already written too much. Think about the first loop

for (var i = small; i < arr.length; i++) {
        if (i < big) {
            arr.push(i);
    }
}

I believe your idea is to start the loop at the lower number, then add all the numbers between small and big, exclusive. I assume that because it makes sense, but there’s a problem. For an array of size 2, this will only run twice. When small and big only have two numbers in between them, like [1,4], that works fine. When the numbers are farther apart, like [5,10], it adds two numbers and then quits out. Your code almost works, though. Here are some hints:

  • Freebie - get rid of the if clause in your loop.
  • Think about why you had the if clause there in the first place. It’s not a bad idea, but where in the loop declaration would it be more useful?
  • You’ll have to make one more slight change to the loop declaration to get it to work. What’s the first number your loop will add to the array?
  • Do the other two for loops do anything for you? What will the outcome of the first loop be?
1 Like

end result
for (i = small+1; i<big; i++){

arr.push(i)
}
return arr.reduce(function(a.b){
return a+b}

1 Like

@MN4PVK4 - sure, that wasn’t an attempt to insult your intelligence. However, it is evidenced in your reply that it wasn’t a trivial change. If we discuss the problematic lines of code in forum instead of just wholesale fixes in code.

This will help you in the long run and also others who have similar problems.

Anyway, on to the problem at hand…

The function you’ve defined function sumAll(arr) is achieved simply with

return newArr.reduce(function(a, b){
return a+b;
}

I’m not sure what the other part of the code is trying to achieve here:

 var newArr = [];
 var big = Math.max(arr[0],arr[1]);
 var small = Math.min(arr[0],arr[1]);

  for (var i = small; i<arr.length;i++){
    if(i < big){
       arr.push(i);
   }
}
for (var j = big; i<arr.length;i++){
if(j > small){
arr.push(j);

}
}

for (var n = small; n<arr.length; n++){
if(n !== arr[i]) {
newArr.push(n);
}
}