Welcome to the Treehouse Community

The Treehouse Community is a meeting place for developers, designers, and programmers of all backgrounds and skill levels to get support. Collaborate here on code errors or bugs that you need feedback on, or asking for an extra set of eyes on your latest project. Join thousands of Treehouse students and alumni in the community today. (Note: Only Treehouse students can comment or ask questions, but non-students are welcome to browse our conversations.)

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and a supportive community. Start your free trial today.

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

Jesse Dispoto
seal-mask
.a{fill-rule:evenodd;}techdegree seal-36
Jesse Dispoto
Front End Web Development Techdegree Graduate 14,537 Points

Can't figure out where to put a loop in order to continuously prompt user...

let message = " ";
let html = " "
let answer;

answer = prompt("Search student records. Enter a name or type quit to exit the program.")

//for each index in array         
for (let i = 0 ; i <students.length ; i++){
html+= "<ul>"
//for each property value within each object
  for (let student in students[i]){

//if user answer  equals the name property of individual object within loop
    if (answer == students[i].name){

  //displays name as heading
            if (student == "name"){
              html+= "<h3>" + student + ": " + students[i][student] + "</h3>"
            }
//displays following characteristics in a list 
            else {
           html+= "<li>" + student + ": " + students[i][student] + "</li>" 
            }

          }
//if user answer is quit 
    else if (answer == "quit"){
          break
    }
  }
    html+= "</ul>"
}

document.write(html);

This is as far as I got regarding this challenge. I am having a really hard time trying to get this in a loop so the user is shown a prompt, even after you type the name, like in Dave's video. Right now, If you type a name, it displays the info on the page but then that's it. The user is not prompted to input another name or quit. And if you type quit OR anything else that is not a name, the program quits. When I try to put it in a while loop or do...while loop, it just creates an infinite loop or doesn't display the right info. Can someone help me out and suggest what I would need to do in order to continuously be display the prompt?

1 Answer

Steven Parker
Steven Parker
210,423 Points

Right now, you have a loop to print the properties, but to prompt more than once you will need to enclose everything in another loop.

If you're having trouble with an outer loop never stopping, try sharing that code so we can give you a more specific answer.