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 trialLee Cockcroft
5,147 PointsLoops/arrays/objects
Hi Guys,
Could someone help me please. I have understood everything individually so far, loops, arrays,objects, however, putting them together, my head is about to explode. I kind of lost it on the last couple of lessons of the javascript course. So i've attempted to do something slightly different, to see if i could do this with my own knowledge. Basically I have while looped a prompt box, asking to select a name. I am trying to list the full object when the name is typed. however it is printing {object object} could someone take a look and explain what i need to do and why please, also if possible explain the for(prop in students) method please.
Thanks
<!DOCTYPE html>
<htl>
<head>
<meta charset="utf-8">
</head>
<body>
<div id="output">
</div>
<script type="text/javascript">
var person={
name: "lee",
age: 31,
skills: ["html","css","javascript"]
};
for (prop in person) {
console.log(prop,":",person[prop]);
}
var students=[
{
name: "Lee",
track: "javascript",
Achievements: 1000,
points: 250
},
{
name: "john",
track: "html",
Achievements: 100,
points: 200
},
{
name: "pete",
track: "css",
Achievements: 10,
points: 25
},
{
name: "james",
track: "php",
Achievements: 2000,
points: 290
},
{
name: "steve",
track: "java",
Achievements: 20000,
points: 400
}
];
while(true) {
var testing=prompt("please enter a name");
if(testing==="quit"){
break;
}
for(var i=0;i<students.length;i++){
if(testing===students[i].name) {
document.write(students[i]);
}
}
}
2 Answers
gregsmith5
32,615 PointsEach student in your students array is an object, and your browser doesn't know how to display that textually. Try this
if(testing===students[i].name) {
var currentStudent = JSON.stringify(students[i]);
document.write(currentStudent);
}
The function JSON.stringify
takes an object in JSON and turns it into a string, which your browser will have no trouble displaying.
You can read a for...in
loop such as for(prop in person)
like this:
for each property in the person object, perform the following
Remember that a property is the same thing as the object's key. The person
object has 3 properties: name, age, and skills. The loop is going through those properties and displaying the contents in your browser's console. If you want, you can delete the person
object and the following for...in
loop and the rest of your code will still work.
Lee Cockcroft
5,147 PointsThank you for your response! need this to sink in ,before I am able to move on! The var currentStudent = JSON.stringify(students[i]); worked :) Lern something new everyday.
Thanks a lot for your help
Jacob Mishkin
23,118 PointsJacob Mishkin
23,118 PointsEdited formatting.