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

Not sure why my answer is wrong, I practically copied the code in the video

Hi,

I am not sure what is wrong with my code, I have been comparing the video code and my code for over 30 minutes and cannot understand where the problem is.

Here is my code: https://w.trhou.se/hl1mfhgg1p

Thank you!

Seth Kroger
Seth Kroger
56,413 Points

I forked your snapshot and it seems to run fine in preview other than not handling the "not found" case. What trouble are you having with getting it to work?

Seth Kroger ,

(I hope I tagged you right there) The problem I am having is nothing is printing to the page for me. I get the prompt on the page, and I can enter something (it will just repopulate the prompt) or type Quit (it will quit the prompt). But the goal of the exercise is to print to the page whichever Name I enter's object data, and that is not happening.

Seth Kroger
Seth Kroger
56,413 Points

Your code won't update the display if the name isn't found in the student records you're using a name that exists there. Also capitalization matters, so make sure it is capitalized correctly. If you want the search to be case-insensitive you can add .toLowerCase() to both sides of the conditional.

3 Answers

Damien Watson
Damien Watson
27,419 Points

Hey Dave,

One thing I have been noticing with this example is that nothing gets shown until you quit. Apparently this is because the 'prompt' being called so quickly stops the page from refreshing and therefore you don't see the 'print()'.

You could fix this issue by replacing the 'while(true) {}' loop with a function that gets called from a timeout. It's a little more complicated, but I have added it below.

The theory is that after the print happens, there is a slight pause before asking for the next students name. This gives the browser enough time to refresh.

// student_report.js

var message = '';
var student;
var search;

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

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

function promptUser() {
  clearTimeout(timer);
  blnLoop = true;
  search = prompt('Type Name or Quit');
  if (search === null || search.toLowerCase() === 'quit') {
    blnLoop = false;
  }
  if (blnLoop) {
    for (var i = 0; i < students.length; i += 1) {
     student = students[i];
     if (student.name.toLowerCase() === search.toLowerCase()) {
       message = getStudentReport( student );
       print(message);
      }
    }
    timer = setTimeout(promptUser, 100);
  }
}

var timer = null;
promptUser();

Why would I need to add more code if I have the same exact code as the video example?

Damien Watson
Damien Watson
27,419 Points

You are not the first person to have this problem, and others have mentioned that it doesn't work the same as in the video. Maybe its a question for the teacher to answer as he may have a different set up.

Good luck.

Joanita Cordova
Joanita Cordova
9,761 Points

Damien Watson I've the same problem and adding the function promptUser() as you posted works for me too! However, not sure what clearTimeout(timer); and blnLoop = true; are. Can you explain it? Thanks a lot!

Damien Watson
Damien Watson
27,419 Points

Hi Joanita, sure, I will eplain below:

clearTimeout(timer);

The variable 'timer' is set using 'setTimeout()'. To stop this timer and 'clear' it, you use 'clearTimeout()'. It is not necessarily needed in this instance as setTimeout is only executed once per call, but it is good practice to make sure the 'timer' is reset. Sometimes you may want to cancel the timer even though it hasn't executed yet, 'clearTimeout()' will stop the timer running and clear it.

blnLoop = true;

I set 'blnLoop' to true, so that it will continue to loop (ask questions), unless 'null' or 'quit' is entered which will set it to false and stop the loop.

blnLoop = true;
search = prompt('Type Name or Quit');
if (search === null || search.toLowerCase() === 'quit') {
  blnLoop = false;
}
if (blnLoop) {
  ...
}
Nouh Ahmed
Nouh Ahmed
7,085 Points

Do you see there is extra curly braces.