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

Matt Singleton
Matt Singleton
4,467 Points

Prompt shows twice

Code works for all but one thing; the prompt shows twice, first time it doesn't react regardless of input, and then works as expected the second time.

var message = '';
var student;
var query;


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

while (true) {
    query = prompt('Enter a student name to view report')
    if (query === null || query.toUpperCase() == 'QUIT') {
        break;
    } else {
        for (i=0; i<students.length; i++) {
            student = students[i];
            if (query.toUpperCase() == student.name.toUpperCase()) {
                message += '<h2>Student: ' + student.name + '</h2>';
                message += '<p>Track: ' + student.track + '</p>';
                message += '<p>Points: ' + student.points + '</p>';
                message += '<p>Achievements: ' + student.achievements + '</p>';
                print(message);
            }
        }
    }
}

Any help will be appreciated.

Jonathan Grieve
Jonathan Grieve
Treehouse Moderator 91,253 Points

No problem.

Do you have output from the div in the html then? :)

This one seems to keep looping the prompt box, until you type quit, so it seems that's fine.

I thought it might have something to do with something missing from the print function

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

But it's been a while since I tackled this :)

1 Answer

Jonathan Grieve
MOD
Jonathan Grieve
Treehouse Moderator 91,253 Points

I just put your JS Code through Codepen and it seemed to work fine,. The prompt only turned up once. Although I don't have your HTML.

The only thing I'd suggest is close the following line with the semi colon.

    query = prompt('Enter a student name to view report');
Matt Singleton
Matt Singleton
4,467 Points

Here's the HTML.

<!DOCTYPE html>
<html lang="en">
<head>
  <meta charset="UTF-8">
  <title>Students</title>
  <link rel="stylesheet" href="css/styles.css">
  <script src="js/students.js"></script>
<script src="js/student_report.js"></script>
</head>
<body>
<h1>Students</h1>
<div id="output">

</div>
<script src="js/students.js"></script>
<script src="js/student_report.js"></script>
</body>
</html>

I've just tried it in CodePen too, and the problem doesn't exist. Very confused.

Jonathan Grieve
Jonathan Grieve
Treehouse Moderator 91,253 Points

Are you working with this in Workspaces by any chance? There might be some sort of bug causing the prompt to appear the second time.

Works properly for me although there''s no output on the HTML, i expect you're aware of that? :)

Matt Singleton
Matt Singleton
4,467 Points

I'm having the issue while running it locally on Chrome. I had an issue with Workspace not linking the two JS files (both were linked in the index file).

I think I'll clear cookies, and reinstall Chrome to see if that fixes these issues. It's jarring the workflow :(

Also I have no idea why there's no HTML output for you. Here's the CodePen link: http://codepen.io/anon/pen/JGNPOg

Matt Singleton
Matt Singleton
4,467 Points

I've just noticed I had the script links in the <head>, so they were being loaded before the html was displayed! I did this when I was trying to solve the issue with Workspaces not running the code in the two external JS files.

Thanks anyway!