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 trialnicolo orcine
3,138 PointsNeed some clarifications
Hello, when i run this code e get a print in the browser of this one. i don't i understand why i have a print of undefined. thanks.
Students
undefined
Nico
Jessa
Duke
Gilbert
Dukepogi
<!------------------------------------------------------------------------>
var List;
var names;
var achiev = 0;
var earned = 0;
function print (storage){
document.write (storage);
};
var details = [
{Name: "Nico", Track: "Front-end Developer", Achievement: achiev, Points: earned},
{Name: "Jessa", Track: "Accountant", Achievement: achiev, Points: earned},
{Name: "Duke", Track: "CEO", Achievement: achiev, Points: earned},
{Name: "Gilbert", Track: "Manager", Achievement: achiev, Points: earned},
{Name: "Dukepogi", Track: "Chairman", Achievement: achiev, Points: earned}
];
for (var i = 0; i <details.length ; i += 1) {
names += '<p>'+ details[i].Name + '</p>';
}
print(names);
nicolo orcine
3,138 Pointsthe word "student" where in the html file. :)
2 Answers
andren
28,558 PointsIt likely comes from the fact that you use += on the names
variable without first assigning anything to it. If you assign an empty string to the variable when you create it. Like this:
var names = "";
Then you should not get undefined in the beginning.
The reason this occurs in the first place is that when you use += to add a string to a variable, JavaScript will automatically convert the variable to a string if it is not already defined as such. When an undefined
variable (a variable that has no value assigned to it) is converted it gets turned into a string actually containing the word undefined.
By assigning it an empty string you avoid this issue since JavaScript no longer needs to convert the variable before it adds the value.
nicolo orcine
3,138 PointsThanks a lot! Very helpful!
Robert Anthony
19,952 PointsRobert Anthony
19,952 PointsIs this ALL the code? Where do you print the word "Students"?