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 JavaScript Loops, Arrays and Objects Tracking Multiple Items with Arrays Two-Dimensional Arrays

Oliver Sewell
Oliver Sewell
16,425 Points

I have decided to do the code this way i was wondering if theirs anything wrong/bad about it as its different to the vid

var studentScores = [
['oliver' , 10],
['tom' , 20],
['craig', 38],
['emma', 15]
];


function listScores() {
  var listHtml = '<ol>';
  for(var i = 0; i < studentScores.length; i+=1) {
  listHtml += '<li>' + studentScores[i][0] + ' got ' + studentScores[i][1];
  } 
  listHtml += '</ol>';
  document.write(listHtml);
}

listScores(studentScores);
nico dev
nico dev
20,364 Points

Hi Oliver,

I know it's late, but just in case, don't forget to close each <li> tag. You do that just adding " + '</li>' " after the "studentScores[i][1]".

1 Answer

Kevin Goudswaard
Kevin Goudswaard
11,061 Points

The only thing I can really see with this is that you have many instances where you're adding on to your listHtml variable. It took me a second to find out what it was doing, and when I did it made sense. So basically by virtue of your design, you are making future developers "crack" your code, which is intrinsically a bad practice. I fiddled with it for a while, and I'll admit it challenged me! But I re-wrote it using dot notation which is no better in essence to what you wrote except its a little more straightforward. Here is the code rewritten that way:

var studentScores = [
['oliver', 10],
['tom', 20],
['craig', 38],
['emma', 15]
];

var list = [];
var element = document.getElementById("mylist");

function listScores() { 
  for (var i = 0; i < studentScores.length; i++) {
  list += studentScores[i][0] + ' got ' + studentScores[i][1] + "<br></br>";
  }
  element.innerHTML = list;
}

listScores();

This includes an html document with a H1 element with the ID = "myList"

Thoughts? better? worse? :)

Kevin

Oliver Sewell
Oliver Sewell
16,425 Points

thanks for the answer kevin , im not too sure :) im abit of a newb when it comes to javascript so i'm just practicing