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 Multiple Items with Arrays Useful Array Methods

Results don't return unless I type quit or cancel prompt

var message;
var student;
var search;

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

  function getStudentReport ( 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;
}

while (true) {
var search = prompt("What student would you like to search for? Type 'quit' to exit.");
  if (search === null || search.toLowerCase() === "quit") {
break;
}
 for (var i=0; i<students.length; i +=1) {
 student = students[i];
if (student.name === search) {
     message = getStudentReport(student);
print(message);
}
}
}

When I run the above code the prompt does not stop returning even if I type out a correct name. It does this even if I type 'break;' and place it in the last conditional statement above.. It returns the results only if I cancel or type quit in the prompt box.

Cheo R
Cheo R
37,150 Points

To format you code, you only need to use three backticks ```, followed by javascript , your code, and three more back ticks.

```javascript

code

```

Thank you, Cheo.

1 Answer

Hi, Michael.

When you open the Workspace for this video, you'll notice that Dave posted a comment about this problem:

"/* Important note The behavior of most browsers has changed since this video was shot, so you won't see the same thing as I demonstrate in the video. In the video, you'll see that my script is able to print out to the browser using document.write( ) while inside a loop.

Most browsers no longer do that: they wait until the loop finishes and then they print to the window. So, you'll see a blank page until you type quit in the prompt window — then you'll see all the output printed to the screen.

Sorry for the confusion, and we'll update the video soon. */"

Now, to solve this problem, replace all instances of "print" with "alert" and you'll see your list as well as the other messages.

Example:

From

print( instock.join(', '));

To

alert( instock.join(', '));