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

Can't get the prompt (for student) loop functioning properly

(1) This is my first question, so please be gentle when reprimanding me if I need to express my question in a better way (I've been historically pummeled on Stack Overflow).

(2) I'm including all my "search" code below in anticipation of a possible "can I see the code?"

(3) I was able to get the student details to print without using a loop. In other words, with the "1 prompt and out" approach, I could enter a name and print the details for that one student. This worked perfectly, as far as I could tell.

(4) Incorporating that prompt into a loop is a different matter. What I tried to do was prompt the user for a name, print the details to the screen for that student, prompt the student for another name, print the details to the screen for that second student, and so on, until "quit" is entered.

(5) I'm experiencing something other than what I expected. I prompt for a student name, but then the prompt appears again, asking for another student name. Then the prompt appears again, asking for another student name. When I finally "quit", the screen displays the details for those three students that I selected consecutively, as if it were in some queue.

(6) I tried a few things, but can't get the details for a specified student to appear immediately after entering the name in the prompt box.

I'm just looking for ideas at this point, thanks.

*** Code ****

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

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

function searchForStudent(answer) { for (var i = 0; i < students.length; i += 1) { student = students[i]; if (student.name === answer) { message += '<h2>Student: ' + student.name + '</h2>'; message += '<p>Track: ' + student.track + '</p>'; message += '<p>Points: ' + student.points + '</p>'; message += '<p>Achievements: ' + student.achievements + '</p>'; return message; } }
}

answer = prompt("Enter name of student");

while (answer !== "quit") { message = searchForStudent(answer); print(message); answer = prompt("Enter name of student"); }

2 Answers

Steven Parker
Steven Parker
243,656 Points

(1) We are all students here to learn. No one should be "reprimanding" anyone for asking questions.

(2) Including the code is great, but formatting makes it readable. Use the instructions for code formatting in the Markdown Cheatsheet pop-up below the "Add an Answer" area. :arrow_heading_down:   Or watch this video on code formatting.

(3) So far so good...

(4) (5) Modern browsers generally don't render the page until the JavaScript program has finished. So it makes sense that you would only see the output (and all of the output) after you quit the program.

(6) As you progress in the courses, you will learn other ways of creating interactive programs using input controls and forms. These techniques will allow you create immediately responsive actions.

Hi Steven, thank you very much for your comment!! Hey, on an unrelated note, I modified my code to account for multiple instances of multiple students having the same name (i.e. 2 students named Dave and 2 students named Jordan). I was going to post this somewhere, so that someone else could use it if they chose, but not sure where to do this.

Steven Parker
Steven Parker
243,656 Points

There's no separate space for sharing, the forum serves that purpose also even though the features are optimized for questions and answers.

Thanks, Steven!