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

shuffling displayed treehouse badges using jquery

l am just finished the ajax course and tried doing something with the treehouse api. l ended up with a code like below which works fine and displays the badges and points that l have earned on treehouse.

$(document).ready(function(){    
var treehouseAPI = "https://teamtreehouse.com/katawura.json";
  $('#badges').html('<h1>Loading....</h1>');
 var  treehouseOptions = { format: "json" };
    function displayBadges(data){
      var badgesHTML = "";
      badgesHTML += '<h1>points on treehouse' + data.points.total +'</h1>';
      badgesHTML += '<ul>';
      $.each(data.badges, function(j, badge){ 
      badgesHTML += '<li class="grid-25 tablet-grid-50">';
      badgesHTML += '<img src="' + badge.icon_url + '"></a></li>';
      });
      badgesHTML += '</ul>';
      $('#badges').html(badgesHTML);
    }
    $.getJSON(treehouseAPI,treehouseOptions, displayBadges);
  });

But wanted to take it further making the badges to display in a random order. However l seem to be failing. any ideas how l can accomplish this please. the code that l tried is below Andrew Chalkley Dave McFarland

$(document).ready(function(){    
var treehouseAPI = "https://teamtreehouse.com/katawura.json";
  $('#badges').html('<h1>Loading....</h1>');
 var  treehouseOptions = { format: "json" };
    function displayBadges(data){
      var badgesHTML = "";
      badgesHTML += '<h1>points on treehouse' + data.points.total +'</h1>';
      badgesHTML += '<ul>';
      imageOf = data.badges[Math.floor(Math.random()*data.badges.length)];
      $.each(imageOf, function(j, badge){ 
      badgesHTML += '<li class="grid-25 tablet-grid-50">';
      badgesHTML += '<img src="' + badge.icon_url + '"></a></li>';
      });
      badgesHTML += '</ul>';
      $('#badges').html(badgesHTML);
    }
    $.getJSON(treehouseAPI,treehouseOptions, displayBadges);
  });

1 Answer

Dave McFarland
STAFF
Dave McFarland
Treehouse Teacher

Kelvin Atawura,

The badges property in the returned JSON object is an array. You can use a function to randomize the order of the badges. Here's a compact array shuffle function I found on Stackoverflow:

function shuffle(o){
    for(var j, x, i = o.length; i; j = Math.floor(Math.random() * i), x = o[--i], o[i] = o[j], o[j] = x);
    return o;
}

I haven't test this code, but I believe you could re-write your code like this to get a randomized list of badges:

$(document).ready(function(){    
  var treehouseAPI = "https://teamtreehouse.com/katawura.json";
  $('#badges').html('<h1>Loading....</h1>');
  var  treehouseOptions = { format: "json" };
  function shuffle(o){
     for(var j, x, i = o.length; i; j = Math.floor(Math.random() * i), x = o[--i], o[i] = o[j], o[j] = x);
     return o;
  }
  function displayBadges(data){
      var badgesHTML = "";
      badgesHTML += '<h1>points on treehouse' + data.points.total +'</h1>';
      badgesHTML += '<ul>';
      var randBadges = shuffle(data.badges)
      $.each(randBadges, function(j, badge){ 
         badgesHTML += '<li class="grid-25 tablet-grid-50">';
         badgesHTML += '<img src="' + badge.icon_url + '"></a></li>';
      });
      badgesHTML += '</ul>';
      $('#badges').html(badgesHTML);
  }
  $.getJSON(treehouseAPI,treehouseOptions, displayBadges);
});

Let us know if it works!

Thanks Dave McFarland it works perfectly.