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

Using my getStudentReport-function introduces a problem

My student-search is working as it should, however when I try to use the getStudentReport instead of a string variable in the loop the program will only return the details of the last name that was entered, does anyone know why this is , and how to fix it ? Thanks! These are my function and code:

function getStudentReport(student){ var report = "<h2> 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){ print(html); search = prompt("Search for a student to view his/her data or type 'quit' "); if(search==="quit"){ break; }

for(let i = 0; i <students.length; i++){
  student = students[i];

  if(search===student.name){
    studentFound = true;
    // html = getStudentReport(student);
    // print(html);
    html += "<h2> Student: " + student.name + "</h2>";
    html += "<p>Track: " + student.track+ "</p>";
    html += "<p>Achievements: " + student.achievements+ "</p>";
    html += "<p>Points: " + student.points+ "</p>";
    //when I use getStudentReport() here in stead of the html-string I get only the last name entered returned to the screen 
  }
}
if (studentFound===false){
  html =  search + " is not on the list";
}

}

Steven Parker
Steven Parker
229,708 Points

This must not be all the code.

There's a reference to a "print" function, and to a variable named "html", but neither is defined here.

If you're using the workspaces, you can share everything at once if you make a snapshot of your workspace and post the link to it here.

Also, when posting code, always use the instructions for code formatting in the Markdown Cheatsheet pop-up below the "Add an Answer" area. :arrow_heading_down:

2 Answers

Steven Parker
Steven Parker
229,708 Points

There's still more code.

The students array, the HTML portion, and the CSS. This would be a good situation for that workspace snapshot!

However, I spotted the problem anyway. Your individual lines all use a concatenating assignment ("+="); but when you call the function you have a normal assignment, which replaces anything previously stored. Just use the same operator with the function:

        html += getStudentReport(student);

Thanks, that fixed it !

you're right, something went wrong there, this is the full code:

let html = '';
let student;
let studentFound= false;
let search;

const print = (message) =>{
  var divOutput = document.getElementById('output');
  divOutput.innerHTML = message
}
const getStudentReport = (student)=> {
  var report = "<h2> 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){
  print(html);
  search = prompt("Search for a student to view his/her data or type 'quit' ");
  if(search==="quit"){
    break;
  }

    for(let i = 0; i <students.length; i++){
      student = students[i];

      if(search===student.name){
        studentFound = true;
        // html = getStudentReport(student);
        // print(html);
        html += `<h2> Student: ${student.name}  </h2>`;
        html += `<p>Track: ${student.track} </p>`;
        html += `<p>Achievements: ${student.achievements} </p>`;
        html += `<p>Points: ${student.points} </p>`;
        //when I use getStudentReport() here in stead of the html-string I get only the last name entered returned to the screen
      }
    }
    if (studentFound===false){
      html =  search + " staat niet op de lijst";
    }
}