Welcome to the Treehouse Community

Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.

Start your free trial

JavaScript

Tyler Taylor
Tyler Taylor
17,939 Points

How to display total points?

Just finished the "Build a simple site with Node.js" course. I decided to play around a little with the code to learn it better.

What I'm trying to do is show the total number of points. I can't seem to figure out how to do it though.. if I try to call anything after profileJSON.points like the javascriptPoints set up, profileJSON.points.JavaScript, I get errors.

I'm assuming I need a loop to get through the points, but I'm not sure how to set that up. Any help would be awesome

studentProfile.on("end", function(profileJSON){
    //show profile


    //Store the values which we need
    var values = {
      avatarUrl: profileJSON.gravatar_url, 
      username: profileJSON.profile_name,
      badges: profileJSON.badges.length,
      javascriptPoints: profileJSON.points.JavaScript,
      totalPoints: ???
    }

1 Answer

In this case, you just need to grab the profileJSON.points.total property to get the total number of points for that user. Is that what you were after?

Otherwise, you would need to loop over the properties in the profileJSON.points object and sum the values, not including the one for the total property.

Something like this:

var totalPoints = 0;
for (var topic in profileJSON.points) {
  if (topic !== 'total') {
    totalPoints += profileJSON.points[topic];
  }
}
// totalPoints should now hold the same value as profileJSON.points.total
Tyler Taylor
Tyler Taylor
17,939 Points

Ahh, thank you! Was having a major brain fart on that one.