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

Anthony Scott
PLUS
Anthony Scott
Courses Plus Student 9,001 Points

Only works if I press cancel

So I finially got my code to work... sort of. It only works after I press the cancel button. I've read this is due to a browser change but I still can't get it to work. AND the video should have been updated by now.

var students = [ 
  { 
   name: 'Dave',
    track: 'Front End Development',
    achievements: 158,
    points: 14730
  },
  {
    name: 'Jody',
    track: 'iOS Development with Swift',
    achievements: '175',
    points: '16375'
  },
  {
    name: 'Jordan',
    track: 'PHP Development',
    achievements: '55',
    points: '2025'
  },
  {
    name: 'John',
    track: 'Learn WordPress',
    achievements: '40',
    points: '1950'
  },
  {
    name: 'Trish',
    track: 'Rails Development',
    achievements: '5',
    points: '350'
  }
];

var search;
var message = "";
var student;

function print(message) {
    document.write(message);
}

function getStudentReport( student ) {
    message = "<p><h1>Student: " + student.name + "</h1></p>";
    message += "<p>Track: " + student.track + "</p>";
    message += "<p>Achievments: " + student.achievements + "</p>";
    message += "<p>Points: " + student.points + "</p>";
    return message;
}

while (true) {
    message = "";
    search = prompt("Enter a student name. Type quit to exit.");
    search = search.toLowerCase();
    if (search === "quit" || search === null) {
        break;
    } 

        for (var i = 0; i < students.length; i += 1) {
            student = students[i];
            if ( search.toLowerCase() === student.name.toLowerCase() ) {
                message += getStudentReport( student );
                print(message);
            }            
    }       
}

3 Answers

Steven Parker
Steven Parker
231,110 Points

The page should render if you cancel or enter "quit".

Either way should end the program. And a modern browser will render the page after the JavaScript code finishes.

You forgot to include a link to the course page, but if I remember correctly, this is discussed in the "teacher's notes" section.

If you need to show output while a program is still running you can use the alert function. Or you could have a fancier page where all the interaction is done on the page itself using input fields and buttons. Techniques for this will be discussed in later lessons.

Anthony Scott
PLUS
Anthony Scott
Courses Plus Student 9,001 Points

Here is the video https://teamtreehouse.com/library/javascript-loops-arrays-and-objects/tracking-data-using-objects/the-student-record-search-challenge

It does not render the code unless I click cancel. If I click OK, it just gives me another prompt box. Interestingly when I run the teacher's code from the solution folder, it does the same thing.

Steven Parker
Steven Parker
231,110 Points

That's the behavior you can expect from a modern browser, when the video was made typical browser behavior was a bit different. But it should also render if you enter "quit".

Anthony Scott
PLUS
Anthony Scott
Courses Plus Student 9,001 Points

ah ok. Just a little confusing because the video is different.