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

matt mccherry
matt mccherry
4,493 Points

Displaying first result twice? (Extra challenge problems)

For some reason, my first result is displayed twice with this coding. Not entirely sure why! So close it's fustrating!

var student;
var search;
var message = "";
var totalStudents = students.length;


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

function outputReport(student) {
  var report = "<li>Name: " + student.name + "</li>";
  report += "<li>Track: " + student.track + "</li>";
  report += "<li>Achievements: " + student.achievements + "</li>";
  report += "<li>Points: " + student.points + "</li>";
  return report;
}

while (true) {
    search = prompt("What name would you like to search?")
    if (search === null || search.toLowerCase() === "quit") {
      break;
    }
  for (var i = 0; i < students.length; i++) {
    student = students[i];
    if (search.toLowerCase() === student.name.toLowerCase()) {
      message += outputReport(student);
      print(message);
    } else {
      totalStudents -= 1;
    }
    if(totalStudents === 0) {
      message+= "<h2>" + search + " is not in the list of names.</h2>";
      print(message);
    }
}
}

-Matt

1 Answer

matt mccherry
matt mccherry
4,493 Points

This doesn't seem optimal but it fixed my problem, running a while within a while.

Any points would be appriciated anyway!

var student;
var search;
var message = "";
var totalStudents = students.length;


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

function outputReport(student) {
  var report = "<li>Name: " + student.name + "</li>";
  report += "<li>Track: " + student.track + "</li>";
  report += "<li>Achievements: " + student.achievements + "</li>";
  report += "<li>Points: " + student.points + "</li>";
  return report;
}

while (true) {
    search = prompt("What name would you like to search?")
    if (search === null || search.toLowerCase() === "quit") {
      break;
    }
    while (totalStudents > 0) {
        for (var i = 0; i < students.length; i++) {
        student = students[i];
        console.log(student.name);
        if (search.toLowerCase() === student.name.toLowerCase()) {
            message += outputReport(student);
            print(message);
            totalStudents -= 1;
        } else {
        totalStudents -= 1;
        }
    if(message = "") {
      message+= "<h2>" + search + " is not in the list of names.</h2>";
      print(message);
    }
}
}
}

Additionally, I would of expected this code to work like this also but it doesn't any explanation?

while (true) {
    search = prompt("What name would you like to search?")
    if (search === null || search.toLowerCase() === "quit") {
      break;
    }
    while (totalStudents > 0) {
        for (var i = 0; i < students.length; i++) {
        student = students[i];
        console.log(student.name);

// changes made within this if

        if (search.toLowerCase() === student.name.toLowerCase()) {
            message += outputReport(student);
            print(message);
        } 
        totalStudents -= 1;
        }

//displays first Jody twice for some reason when done this way.

    if(message = "") {
      message+= "<h2>" + search + " is not in the list of names.</h2>";
      print(message);
    }
}
}