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 trialJosie Nagy
14,992 PointsUndefined issue
Hi everyone! I'm stuck trying to understand what's going on here.
My issue: instead of displaying the name, as it should, it just shows me this in the browser:
Student: undefined
Here is my solution so far:
var message = "";
var student;
function print(message) {
var outputDiv = document.getElementById("output");
outputDiv.innerHTML = message;
}
for (var i = 0; i < students.length; i += 1) {
var student = students[i];
message += '<h2>Student: ' + students.name + '</h2>';
}
print(message);
My JSON file:
var students = [
{
name : "Josie",
track : "Python Development",
achievements : 108,
points : 11500
},
{
name : "Caleb",
track : "iOS Development with Swift",
achievements : 68,
points : 18099
},
{
name : "Andal",
track : "Design Thinking",
achievements : 73,
points : 9960
},
{
name : "Tara",
track : "Front End Development",
achievements : 530,
points : 40021
},
{
name : "Jonathan",
track : "JavaScript Basics",
achievements : 44,
points : 2504
}
];
My index.html file:
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Students</title>
<link rel="stylesheet" href="css/styles.css">
</head>
<body>
<h1>Students</h1>
<div id="output">
</div>
<script src="./js/student-records.js"></script>
<script src="./js/students.js"></script>
</body>
</html>
1 Answer
anthony amaro
8,686 Pointsone issue is that you are trying to get the name from the array not the object
var student = students[i];
message += '<h2>Student: ' + students.name + '</h2>';
// it should be student.name
you are storing the objects in student variable. students is the array. hope this helps
Josie Nagy
14,992 PointsJosie Nagy
14,992 PointsThanks a lot Anthony, yes that was the issue. I understand the difference now.