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

Can this code be simplified?

My code - https://w.trhou.se/cohmtnq9ua

wondering if it is possible to simplify this code further; specifically by removing the variable studentNotFound and adding an 'if else' or 'else' statement to the loop which asks (student.name !== search) and answers / replaces the prompt with "Sorry, but " + search + " does not exist in our records. Please enter another name." instead of an alert. . . is it possible to use this instead of declaring another variable such as the studentNotFound?

1 Answer

It depends on what you mean by simplified. The quick and lazy solution would be to move the printing outside the for block, and if there's nothing to print it means there was not student matching the search.

Here's a solution that prompts continuously, implements the lazy solution for printing with no student records found, and handles potential students with the same name.

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

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

// function to generate a report for a given student
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;
}

// loop until one of the below conditions are met
while (true) {
  search = prompt('Search student records: type a name [Jody] (or type "quit" to end)');
  // if the search is null, empty, or quit - stop the loop
  if (search === null || search === '' || search.toLowerCase() === 'quit') {
    break;
  }
  // for each student, determine if their name matches the search
  for (var i = 0; i < students.length; i += 1) {
    student = students[i];
    if (student.name.toLowerCase() === search.toLowerCase()) {
      message += getStudentReport( student ); // if the name matches, generate a report for them
    }
  }
  if (message == '') { // if no reports were generated, the search name was not in the student records
    print('<h2>There is no student by the name ' + search + ' in our records.');
  } else {
    print(message); // if there are report(s) for the search, print them
    message = ''; // reset the message variable on each loop
  }
}

Like I said, it's probably not the most elegant solution, but it eliminates the need for certain variables like popupWindow or studentNotFound.

Let me know if there's anything that's not clear, or if you find a better solution! Happy coding! :)

Everything makes sense to me, but I don't think this is quite the solution im looking for. Also, when I type in a name that has data to it, it's not printing our their records to their page until I close the prompt. If you see the code I created, it's why I added this line " promptSearch(); //execute function first time - allows Student Records to show while prompt is still active instead of only after it is closed " on line 19. . i'm going to keep messing around with this code..

btw, is it possible to message people on here?

Odd, it prints immediately for me in the background while the prompt is up. Maybe a browser issue? I'm using Firefox on OSX, for reference.

You're going to have a tough time just adding an if or if-else to the loop without saving something to another variable, or moving some expressions outside the for block. You'll run into the problem of printing things incorrectly if you try to put something like (student.name !== search) inside the for block. But there might be another way of looking at it that I'm missing.

Unfortunately, there is no private message functionality for our community at this time. Sorry!

Thank you for your comment Rydavim. Weirdly enough, I tried your code on my Firefox browser as well and its printing the code in the background while the prompt is up. I have been using Google Chrome this whole time (Windows 10) and have never seen anybody mention this til now. . Maybe I should start using Firefox instead :D