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

My solution is totally wrong... but why?

So I tried a completely different approach and it didn't work at all, but I just wanted to clarify why.

var message = ''; var student; var search;

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

do { search = prompt('Search for a student by name, or type quit to exit.'); } while (search !== 'quit' || search !== null)

student = students[students.indexOf(search)];

for (var prop in student) {

message = prop + ": " + student[prop];

}

print(message);

A) Why did my do while loop work at first, but get stuck in an infinte loop when I added the '|| search !== null' ?

B) Did my message not print because I can't access an object's properties by referencing it's array index? Does it have to be it's name?

Wow that's some terrible formatting. Sorry guys.

var message = '';
var student;
var search;

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

do { 
search = prompt('Search for a student by name, or type quit to exit.');
} while (search !== 'quit' || search !== null)

student = students[students.indexOf(search)];

      for (var prop in student) {

      message = prop + ": " + student[prop];

}


 print(message);   

1 Answer

Jonathan Grieve
MOD
Jonathan Grieve
Treehouse Moderator 91,253 Points

Hi there Bonnie,

For the first one try a search for an empty string rather than null. I'm assuming you're checking for an instance where someone types nothing into the input box,

while (search !== 'quit' || search !== "")

Secondly yes I believe Array Indexes don't work with getElementById since with getElementById you're searching one specific element, it doesn't need an array Index to find it. :-)

Try getElementsByTagName :)