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 trial

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

Rishit Shah
Rishit Shah
4,975 Points

Final challenge problem

As a part of the final challenge, i wrote the following code. However it enters into an infinite loop. The page keeps on loading. Please tell me what is wrong

var message = '';
var student;
var answer;

function print(message) {
  var outputDiv = document.getElementById('output');
  outputDiv.innerHTML = message;
}

function getData(student){
  var  report = '<h2>Student: ' + student.name + '</h2>';
     report += '<p>Track: ' + student.track + '</p>';
     report += '<p>Points: ' + student.points + '</p>';
     report += '<p>Achievements: ' + student.achievements + '</p>';
    return  report;
 }

answer = prompt('Enter name' );
while (answer !== 'quit' || answer !== null)
{
    for (var i = 0; i < students.length; i += 1) {
      student = students[i];
      if(answer === student.name)
      {
        message = getData(student);
        print(message);
      }
      else
      {
        message = 'no student found';
        print(message);
        break;
      }
  }
}

//Fixed Code Presentation

How to post Code

1 Answer

akak
akak
29,445 Points

Hi Rishit,

To answer your question "why it loops endlessly" - you have a loop inside a loop. When you break from “for" loop, you’re back into while loop, and since you have prompt outside the loop answer never changes so it will loop endlessly with the word you’ve entered. If you put your prompt inside the while loop you’ll be able to enter other names but quit won’t work since you check the condition before changing the answer to other value. The Dave solution works as he checks condition of the prompt inside while loop and after prompt. Each time while loop… loops he can change the answer and that gets evaluated in if statement. And since he doesn’t have that conditional inside a loop “break;” stops the while loop.

Just a note: you can break while loop from the inside of the for loop. In JavaScript you can name loops and break the one you want. For example:

outerLoop : while (true) {  //endless loop
  innerLoop: for (var i = 1; i >0 ; i++){ // another endless loop
    break outerLoop; // we brake from the while loop
  }
} 

But either way that won’t do much if you don’t restructure your code.

I hope that makes any sense at all :)

Good response. Didn't know about naming loops and breaking from specific ones. Very cool!

I think this would also work with a do... while loop because then you could prompt the first time through, and check the condition afterwards. You would just need to make sure the first time through that it won't do weird stuff if they quit straight away.

Rishit Shah
Rishit Shah
4,975 Points

Thank you for the answer. It did make sense after i read it three times :P

akak
akak
29,445 Points

Glad to hear that :) Iain solution looks neat. I use do... while so rarely I didn't take that into account :)