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

Michael LaCroix
Michael LaCroix
5,828 Points

Only prints after break statement - can't figure out why.

Question is in subject and, honestly, I'm just sick of trying to figure this having spent over 4 hours on a challenge that sounds like it was supposed to take like 20 or 30 mins.

var students = [
  {name: 'Josh', track: 'iOS', achievments: 'Web Banner', points: '1,200'},
  {name: 'Sam', track: 'Android', achievments: 'Web Banner', points: '3,200'},
  {name: 'David', track: 'Java', achievments: 'Web Banner', points: '4,200'},
  {name: 'Adriana', track: 'JS', achievments: 'Web Banner', points: '5,200'},
  {name: 'Alexis', track: 'Ruby', achievments: 'Web Banner', points: '6,200'},
];  

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

while (true) {  
  searchingStudent = prompt('Name of student?');
  if (searchingStudent === 'quit'){
  break;
}
for (var i=0; i<students.length; i+= 1) {
  if (searchingStudent === students[i].name) {
  saughtStudent += '<h2>Name: ' + students[i].name + '</h2>'; 
  saughtStudent += '<p>Track: ' + students[i].track + '</p>';
  saughtStudent += '<p>Achievments: ' + students[i].achievments + '</p>';
  saughtStudent += '<p>Points: ' + students[i].points + '</p>';
  print(saughtStudent); 
}
}
}

Thanks

5 Answers

Dave McFarland
STAFF
Dave McFarland
Treehouse Teacher

Sorry for the confusion Michael LaCroix -- yes, there's been a change since this video was shot to how browser behave. I've added a note in the Teacher's note for this video and plan on reshooting this particular video soon.

Brendan Moran
Brendan Moran
14,052 Points

Very interesting! Is this only for document.write() or is it for other DOM manipulation methods too?

Dave McFarland
Dave McFarland
Treehouse Teacher

Brendan Moran. That's correct -- once the browser enters that while loop that uses the prompt( ) method, you can't manipulate the DOM in any way. Basically, prompt() is good for teaching and practicing simple JavaScript. You'll soon learn more complex ways to get information from users, like pop-up foms, that won't lock up a browser like the prompt() method.

Brendan Moran
Brendan Moran
14,052 Points

Michael LaCroix : sweet vindication. This IS a browser issue. I am not sure if it is an error per se, or just a way that they are changing how the browser interprets function calls in a while loop, but I would say that this current functioning feels like a malfunction. IMO, if you call a function in a while loop, the function should be called and executed, following normal JS control flow. Maybe this behavior was changed in order to prevent deadly endless while loops that are calling big functions every time through, which I guess could lead to some really disastrous results.

Your code works in Firefox, but not in Chrome or Safari. Obviously, something has changed in the last 6 months, because this worked fine for me 6 months ago in Chrome. Dave McFarland -- just an FYI!

Brendan Moran
Brendan Moran
14,052 Points

I can not figure it out. I manipulated the code to something I thought would work... and it didn't. I followed Dave's instructions in the solution video down to the detail, and it didn't work. I remember doing this challenge about 6 months ago and having no problem with it, and now I can't get it working. It has actually got me pretty disturbed at this point. Either there is something very simple I am missing, or we have uncovered a bug in the web.

Perhaps someone smarter can come along and spot what we are missing.

Just some pointers:

Indent your code.

Be careful with your variable naming. You have a message variable that you never use, and you have a "searchingStudent" variable in your while loop that was not declared. The code is functional, but this can make your code hard to read as well as more error prone. Also, I would suggest renaming "searchingStudent" to something like "search" or "userInput" which will semantically much easier to follow.

Brendan Moran
Brendan Moran
14,052 Points

Oh, and I should clarify: I also replaced the whole html building/printing part (the full contents of the IF block) with a console.dir of students[i] -- I did this with your code in its current state as well as with the Dave's solution. Sure enough, the object was displayed in the console every time I entered a name, without having to exit the while loop. So the code is moving on to the IF statement, for sure, but for some reason it is not printing. As you know, what is happening, for whatever reason, is that your print function is only being executed after exiting the while loop. This should simply not be the case. JS control flow is such that when you call the function in your IF statement, the control is handed over to that function until it is complete and exits the function, returning to the last line of the if statement. I may be missing something, but based on your code, the content absolutely should be getting printed to the page, but is simply not. I don't get it.

This is over my head right now. I wonder what Dave McFarland would have to say about it?

Arkadiusz Odrowaz
Arkadiusz Odrowaz
1,354 Points

Och and i was wondering what i did wrong...took me a while before i checked the comment section..damm i really started to doubt if i can do this :)