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 trialyoav green
8,611 PointsMy code outputs only my last object in the array
var students = [
{ name : 'yoav', track: 'JAVA', Achievements : 15, points : 30},
{ name : 'neta', track: 'CSS', Achievements : 9, points : 10},
{ name : 'gidon', track: 'HTML', Achievements : 16, points : 200},
{ name : 'chanan', track: 'Ruby', Achievements : 100, points : 1000},
{ name : 'refael', track: 'Python', Achievements : 30, points : 500}
];
var message = '';
var student;
var search;
function print(message) {
var div = document.getElementById('output');
div.innerHTML = message;
}
function getStudentRaport( student ) {
var reprot = '<h2>Student: ' + student.name + '</h2>';
reprot += '<p>Track: ' + student.track + '</p>';
reprot += '<p>Achievements: ' + student.Achievements + '</p>';
reprot += '<p>points: ' + student.points + '</p>';
return reprot;
}
while (true) {
search = prompt('Type a name of a student. type "quit" to end the program');
if (search === null || search.toLowerCase() === 'quit') {
break;
}
for(var i = 0; i < students.length; i += 1) {
student = students[i];
if (search = student.name) {
message = getStudentRaport(student);
print(message);
}
}
}
I want it to display each one of the names according to the string from the prompt message
3 Answers
Steven Parker
231,248 PointsYou might want to modify the "print" function so that instead of replacing the contents of the div, it adds to it:
div.innerHTML += message; // note += instead of just =
yoav green
8,611 Pointsbut now it outputs all of the objects in the array. i want in to print out only the students.name according the "name" variable in the objects
Steven Parker
231,248 PointsI added a comment to my answer.
yoav green
8,611 Pointsohhhhhh wowwwwwww!! thank you su much!!!!
Steven Parker
231,248 PointsSteven Parker
231,248 PointsAlso, you probably intended to put a comparison (==) here instead of an assigment (=):
if (search = student.name) {