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

Nathaniel Kolenberg
Nathaniel Kolenberg
12,836 Points

Can't get my code to run for this challenge

Hey everyone,

For some reason, I can't seem to let my code run properly for this challenge.

I've written the exact same code as Dave, the instructor but after I fill in a name in the prompt, it keeps popping up and after entering 'quit' nothing appears on the page.

I would really appreciate any help as I'm endlessly scratching my head at this one. Thanks in advance!

This is my code:

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



var students = [
{
    name : "Nate",
    track : "JavaScript Loops, Arrays and Objects",
    achievements : "12",
    points : "15" 
},

{
    name : "Vero",
    track : "JavaScript Loops, Arrays and Objects",
    achievements : "16",
    points : "36"
},

{
    name : "Mijelor",
    track : "JavaScript Loops, Arrays and Objects",
    achievements : "12",
    points : "20" 
},

{
    name : "Capelito",
    track : "JavaScript Loops, Arrays and Objects",
    achievements : "19",
    points : "5" 
},

{
    name : "Kathrin",
    track : "JavaScript Loops, Arrays and Objects",
    achievements : "10",
    points : "20"
}

];

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


function getStudentReport( student ) {
    var report = "<h2 class='strong'>Student: " + student.name + "</h2>";
        report += "<p> Track: " + student.track + "</p>";
        report += "<p> Achievements: " + student.achievements + "</p>";
        report += "<p> Points: " + student.points + "</p>";
        return report;
}

    while(true) {
        search = prompt("Search for a student name or type 'quit' to exit");
        if(search === null || search.toLowerCase() === "quit") {
            break;
        } 
        for ( var i = 0; i < students.length; i += 1 ) {
            student = students[i];
            if(student.name.toLowerCase() === search.toLowerCase()) {
                message = getStudentReport( student );
                console.log(message);
            }       
        }
    }

Best regards,

Nathaniel

3 Answers

Nathaniel Kolenberg
Nathaniel Kolenberg
12,836 Points

P.S.

Just discovered what the problem is. I was using a Google Fonts link in the head of the HTML to render the Lato font-family. When removing this, the JavaScript code seems to work just fine.... ¯_(ツ)_/¯

I guess I'll use a font-family that's already installed on my laptop.

Cheers for your help again!

Best,

Nate

Dave McFarland
STAFF
Dave McFarland
Treehouse Teacher

Hi Nathaniel Kolenberg

The problem is near the end of your script in the for loop. You have this line of code:

console.log(message);

This just prints the message to the JavaScript console. You need to call the print() function to print it to the page. So change that line of code to this:

print(message);
Nathaniel Kolenberg
Nathaniel Kolenberg
12,836 Points

Hi Dave,

Thanks so much for getting back to me.

I accidentally copy pasted a part of a test I was trying out to see if 'message' actually held the information I needed. My 'live' code does contain print(message);.

I've been tinkering a bit more, and what happens now is that when I type a name, nothing but another prompt appears. It's only when I type 'quit' to exit that the name gets written to the page.

I know I'm missing something here but I just can't seem to identify it... Could you please help me out and see what I might be doing wrong / missed?

Would really appreciate it!

I've added my revised code with the print(message); below.

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


var students = [
{
    name : 'Nate',
    track : "JavaScript Loops, Arrays and Objects",
    achievements : "12",
    points : "15" 
},

{
    name : "Vero",
    track : "JavaScript Loops, Arrays and Objects",
    achievements : "16",
    points : "36"
},

{
    name : "Mijelor",
    track : "JavaScript Loops, Arrays and Objects",
    achievements : "12",
    points : "20" 
},

{
    name : "Capelito",
    track : "JavaScript Loops, Arrays and Objects",
    achievements : "19",
    points : "5" 
},

{
    name : "Kathrin",
    track : "JavaScript Loops, Arrays and Objects",
    achievements : "10",
    points : "20"
}

];

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

function getStudentReport (student) {
    var report = "<h2 class='strong'>Student: " + student.name+ "</h2>";
    report += "<p> Track: " + student.track + "</p>";
    report += "<p> Achievements: " + student.achievements + "</p>";
    report += "<p> Points: " + student.points + "</p>"; 
    return report;
}

while (true) {
    search = prompt("Search for student names or 'quit' to exit")
    if (search.toLowerCase() === "quit" || search === null) {
            break;
        }

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

}
Dave McFarland
Dave McFarland
Treehouse Teacher

Hi Nathaniel Kolenberg

Your code works for me. Maybe the prompt dialog is covering the page where the output appears. Try dragging the prompt out of the way and see if the name is written to the page.

I literally just copied and pasted your code into the workspace and it worked.

Nathaniel Kolenberg
Nathaniel Kolenberg
12,836 Points

Thanks again for your help Dave! As you said, I've tried copying the code now in a workspace on Chrome and then it does indeed work! Locally, however, I get the same problem in Chrome as I do in Safari.

Since I'm still quite set on finishing this challenge with the additional enhancements you highlighted in the course, I will continue from a workspace in Chrome.

Also wanted to say thank you for this course - I really enjoy how you break everything down into smaller chunks of easily digestible information. For a beginner like me, this is the best (and most motivating) way to learn!

Best regards,

Nathaniel

Nathaniel Kolenberg
Nathaniel Kolenberg
12,836 Points

Hi Dave,

Thanks a lot for giving it a shot. Good to hear that it works on the workspace for you. I've actually tried it in Sublime text and Chrome / Safari locally and still get the issues. Tried also moving the prompt out of the way but no luck.

I've just now tried it in a workspace and I'm getting the exact same issues. I'm now wondering if it might be a OS / Browser related issue.

In any case, it's good to hear that it did work for you. I shall continue with jQuery courses for now ;)

Cheers and thanks again for your help!

Kind regards,

Nathaniel

Dave McFarland
Dave McFarland
Treehouse Teacher

Ah ha! Nathaniel Kolenberg.

I CAN reproduce this problem, but ONLY in Safari. I've tried from both workspaces and local files on my computer and the program works in both Chrome and Firefox, but I do see exactly what you are describing in Safari.

I'll try to figure out the reason, but not sure if there's something specific to Safari, that I may not be able to overcome.

Sorry for the frustration -- that's a weird bug. But, on the bright side, you're programming is CORRECT!!!!

keep up the good work