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

Saqib Ishfaq
Saqib Ishfaq
13,912 Points

what's wrong with this? why doesn't it work without creating the new function?

var student;
var report;
var search;
function print(message) {
    var outputDiv = document.getElementById('output');
    outputDiv.innerHTML = message;
  }
 while(true){

    search = prompt('type a student name to search the record, and to end type \'quit');
    if (search === null || search.toLowerCase() === 'quit'){
        break;
    }
} 

        for (var i=0; i< students.length ; i+=1){
            student = students[i];
            if (search.toLowerCase() === student.name){   
            report += '<h2>' +student.name+ '</h2>';
            report += '<p>track: '+ student.track+ '</p>';
            report += '<p>achievements: ' +student.achievements+ '</p>';
            report += '<p> points: ' +student.points+ '</p>';

          }
          print(report);
    }

1 Answer

Steven Parker
Steven Parker
229,708 Points

I'm not sure why you would want to avoid creating the function, but it looks like the main issue might be that the loop that does the searching is outside of the loop that asks for a student name.

It needs to be moved inside the other loop so when something other than "quit" is typed, it will look for it and display it.

Saqib Ishfaq
Saqib Ishfaq
13,912 Points

i created the function in the end, but just wanted to see wether it works without one! thanks yeh it worked

Steven Parker
Steven Parker
229,708 Points

If you did not want to have a "print" function you could substitute the call with code that replicates what it does:

//        print(report);    <- replace this with the following line:
          document.getElementById('output').innerHTML = report;