Welcome to the Treehouse Community

The Treehouse Community is a meeting place for developers, designers, and programmers of all backgrounds and skill levels to get support. Collaborate here on code errors or bugs that you need feedback on, or asking for an extra set of eyes on your latest project. Join thousands of Treehouse students and alumni in the community today. (Note: Only Treehouse students can comment or ask questions, but non-students are welcome to browse our conversations.)

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and a supportive community. Start your free trial today.

JavaScript JavaScript Loops, Arrays and Objects Tracking Data Using Objects The Student Record Search Challenge

Mahfuzur Rahman
Mahfuzur Rahman
3,204 Points

browser crashes while running

var students = [ { name: 'Natalie', track: 'iOS', achievments: 34, points: 57474 }, { name: 'Rudy', track: 'Web Design', achievments: 65, points: 4848 }, { name: 'Buffy', track: 'Front-end Develpoment', achievments: 23, points: 9292 }, { name: 'Stanley', track: 'Java', achievments: 94, points: 5858585 }, { name:'Bobby', track: 'HTML/ CSS', achievments: 56, points: 5755 } ];

for (var i = 0; i < students.length; i++) { for (var prop in students[i]) { document.write(prop, ': ', students[i][prop] + '<br>'); } document.write('<br>'); var search=prompt('which student you want to look up for?'); while(search===students[i].name) {document.write(prop, ': ', students[i][prop]+'<br>'); } }

2 Answers

Steven Parker
Steven Parker
210,475 Points

Your code contains an infinite loop in the last few lines:

  while (search === students[i].name) {
    document.write(prop, ": ", students[i][prop] + "<br>");
  }

Assuming the condition allows the loop to start, neither term is changed inside the loop, so the loop will continue indefinitely.

I would guess that this condition was intended to be for an "if" instead of a loop, and that it was also intended to enclose the loop that would output different properties which occurs a few lines before.

Mahfuzur Rahman
Mahfuzur Rahman
3,204 Points

Many thanks Steven Parker. Now it works but only once. How can I use the search variable to run continuously? Help will be appreciated. var search = prompt('which student you want to look up for?');

for (var i = 0; i < students.length; i++) { if (search === students[i].name) {

for (var prop in students[i]) {
  document.write(prop, ': ', students[i][prop] + '<br>');

}

} else if (search === 'quit') {

document.write('You have finished searching');

} }

Steven Parker
Steven Parker
210,475 Points

Proceed ahead to the next video where you will see an example that answers your second question.

Just be aware that most browsers will not display anything until after you quit the program. See the "important update" in the Teacher's Notes section.

Mahfuzur Rahman
Mahfuzur Rahman
3,204 Points

Yeah, I have just finished watching the solution. while (true) {} was completely out of my mind. Thanks a lot.