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

Updated: student not in records- help

Ok, I'm unsure why my code isnt working as it should. If I type a student not in records(the object) it doesnt print anything. I have created a function and moved it to several different places within the program and it still doesn't work as it should. Please help. ALSO someone else commented on me posting my code correctly and I'm still unsure how?? I watched the markdown video and that didn't help. So how do I post my code in a question so that it looks like it would in a text editor? because I keep only posting it as regular text, unsure how to do it correctly. Thanks

var message = '';
var student;
var search;
var noExist = false;

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;
}

while (noExist === false) {
  search = prompt('Type student name to view records. Type "quit" to end.');
   if(search === null || search.toLowerCase() === 'quit') {
     noExist = true;
    break;
  } 
    for (var i = 0; i < students.length; i += 1) {
      student = students[i];
      if(student.name === search) {
        message = getStudentReport(student);
        print(message);
      } else { noExist = true; }
}    function not() {
  if(noExist === true) {
    message = 'There is no student by that name in our records.';
    print(message);
   }
  }      
}

3 Answers

"Wrap your code with 3 backticks (```)"

that's 3 backticks before and 3 backticks after your code. backticks should be to the left of your number 1 key on the keyboard.

Ahh ok thank you! Now any suggestions on how to solve my code problem/why it's not working?

I'm not recalling this challenge too well, but my guess would be it's because you have created a function called not(); that will indeed display the message you want to see, but you haven't actually called this function anywhere.

e.g.

if (noExist === true){ not(); };

the not(); calls the function and function not(){...} just sets up the function for later use, but doesn't actually do anything until we tell it to.

So try putting your function somewhere outside your loop and then calling the function inside your else statement immediately after the noExist is set to true (in other words where you'd want to use this function). Lmk if you're still having issues I'll try to help :)

Hey Thanks for taking the time. I still am having trouble getting this code to work correctly! I took your suggestions and I think I got a little farther but it still prints out the message, "There is no student..." even when that student exist! I've tried putting the function outside of my code like you mentioned and I've reworked it so many different ways, yet still I cant figure out how to work it correctly.

var message = '';
var student;
var search;
var noExist = false;

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 not() {
  if(noExist === true) {
    message = 'There is no student by that name in our records.';
    print(message);
   }
  }
*/

while (noExist === false) {
  search = prompt('Type student name to view records. Type "quit" to end.');
   if(search === null || search.toLowerCase() === 'quit') {
     noExist = true;
    break;
  } 
    for (var i = 0; i < students.length; i += 1) {
      student = students[i];
      if(student.name === search) {
        noExist = false;
        message = getStudentReport(student);
        print(message);
      } else { 
          noExist = true;
          if(noExist === true) {
            message = 'There is no student by that name in our records.';
            print(message);
        }
      }
}

} 

I have an idea about what's happening here...

You have two loops set up. A while loop and a for loop. Your for loop is cycling through all of the students in the students.js array (5 students total). It WILL iterate through each and every student as long as there are students to iterate through. Your "while loop" on the other hand will ONLY stop if it finds a break; or if the final state of var noExist is set to true. Here's where things get tricky... but hopefully I'll be able to explain why your while loop either won't stop cycling when it finds the student you're looking for (e.g. student.name === search) or is giving you mixed results.

Regardless of the conditional inside this for loop it will run over and over again until it runs out of students to check. Let's say you type in "Dave" and that's the first student in the array. You'd think that it would return "Dave" along with his info as per the getStudentReport(); function. Funny thing is... it actually does write to the document body. Your eyes just couldn't see it because your print(message) function is getting overwritten at lightning speed. The innerHTML getting replaced by each iteration of the loop.

To prove this is happening you can try a couple of things. First, set your print(message) function to += message vs. = message. Like this...

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

That way we'll be ADDING the message to the outputDiv rather than overwriting it with each iteration. Your document body should now show each iteration of the for loop as it's performed and return the correct message corresponding to the students[i] currently set to student. So if we type "Dave" now we'll get Dave's student report followed by 4x "There is no student..." Ok that's not exactly what we want, but it get's us closer to understanding where we're messing up.

Another way I can prove this is happening (and point out another problem with your code while we're at is) is by asking for the very last student in the array, Trish. If we look for Trish the for loop will go around each time and set var noExist to true 4 times in a row however, once it get's to Trish it will set noExist to false. The for loop will stop iterating because it's run out of students but our var noExist now equals false! In other words, we will not break out of our while loop and will get sent right back to the prompt so we can start the loop over again! At this rate we'll never get Trish's results... :(

Now that we understand how the for loop is working we can put our outputDiv.innerHTML += message; back to = instead of += since we just want to return the first match. I'll step out of the way here and see if you can come up with a solution. Keep in mind that the way things are now once your for loop stops iterating var message and var noExist will be equal to whatever the last student in the students[i] loop set them to be since those variables too are being overwritten with each case of the loop as it iterates. Hope this helps get you closer. Otherwise congrats on reading this giant wall of text. :)