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 trialColin Sygiel
5,249 PointsI am very close, but something is not working. Please have a looksy!
I am getting an error with line 45: students.js:45 Uncaught ReferenceError: message is not defined(…) This is the line towards the bottom with the <h2> tag but I can't figure out why it's acting up.
/*Accessible information about students - loops, arrays and objects*/
//Group of 5 students (objects) containing name, track, achievements and points
var students = [
{
name: 'Colin',
track: 'vr',
achievements: 100,
points: 34
},
{
name: 'Vira',
track: 'iOS',
achievements: 90,
points: 98
},
{
name: 'Zara',
track: 'android',
achievements: 871,
points: 129
},
{
name: 'Peach',
track: 'web',
achievements: 42,
points: 12
},
{
name: 'Lee',
track: 'ai',
achievements: 900,
points: 123
}
];
//function for printing a message anywhere on the webpage
function print(message) {
var outputDiv = document.getElementById('output');
outputDiv.innerHTML = message;
}
//loop for crawling array and printing properties to screen
for (i = 0; i < students.length; i += 1) {
message += '<h2>Student: ' + students[i].name + '</h2>';
message += '<p>Track: ' + students[i].track + '</p>';
message += '<p>Achievements: ' + students[i].achievements + '</p>';
message += '<p>Points: ' + students[i].points + '</p>';
}
print(message);
1 Answer
Jason Cook
11,403 PointsI see you solved it already, nice job! By the way, I have an alternative version of that bottom functionality you might like to plugin and try (just for experimenting). It dynamically loops through using a nested loop, programmatically, determining the property names.
If you're a code geek like me, you might appreciate it, so here it is (the top array of objects can stay the same - just replace from the print() function down with this alternate version to try).
I know it's not as readable, for maintainability, but it's just for fun and learning purposes :-)
function print (message) {
var outputDiv = document.getElementById("output");
outputDiv.innerHTML = message;
}
var studentList = '';
//Dynamically loop through the array
for(var i = 0; i < students.length; i++) {
for (key in students[i]) {
if (key === "name") {
studentList += '<p class="studentname">' + students[i][key] + '</h2>';
} else {
studentList += '<p>' + students[i][key] + '</p>';
}
}
studentList += '<br>';
}
print(studentList);
Happy coding! :-)
Colin Sygiel
5,249 PointsThanks so much Jason - this is very cool and I appreciate your efforts and enthusiasm. Happy coding to you too :)
Colin Sygiel
5,249 PointsColin Sygiel
5,249 PointsI solved it! Looks like I forgot to declare the message as a variable :)