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 trialMahfuzur Rahman
3,204 Pointsbrowser 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
231,248 PointsYour 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.
Steven Parker
231,248 PointsProceed 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
3,204 PointsYeah, I have just finished watching the solution. while (true) {} was completely out of my mind. Thanks a lot.
Mahfuzur Rahman
3,204 PointsMahfuzur Rahman
3,204 PointsMany 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) {
} else if (search === 'quit') {
document.write('You have finished searching');
} }