Welcome to the Treehouse Community

The Treehouse Community is a meeting place for developers, designers, and programmers of all backgrounds and skill levels to get support. Collaborate here on code errors or bugs that you need feedback on, or asking for an extra set of eyes on your latest project. Join thousands of Treehouse students and alumni in the community today. (Note: Only Treehouse students can comment or ask questions, but non-students are welcome to browse our conversations.)

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and a supportive community. Start your free trial today.

JavaScript JavaScript Loops, Arrays and Objects Tracking Data Using Objects The Student Record Search Challenge

Sandra Vu
Sandra Vu
3,525 Points

Code review- why does this code snippet not work?

Smart strangers could you help review these code lines?

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

 do (
    var search = prompt('type a name [Jody] or type "quit" to end');
    for (var i = 0; i<students.length; i+=1) {
        student= students[i];
        if (search == student.name){
            message+= '<h2>Student: '+student.name+  '</h2>';
            message+= '<p>Track: '+student.track+ '</p>';
            message+= '<p>Points: '+student.points+  '</p>';
            message+= '<p>Achievements: '+student.achievements+ '</p>';
            print(message)
        }
    }
 )
 while(search!=='quit')

3 Answers

Hey Sandra Vu,

I caught some mistakes in syntax declaration of do while loop.You are supposed to use curly braces instead of parantheses in your code.

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

 do {
    var search = prompt('type a name [Jody] or type "quit" to end');
    for (var i = 0; i<students.length; i+=1) {
        student= students[i];
        if (search == student.name){
            message+= '<h2>Student: '+student.name+  '</h2>';
            message+= '<p>Track: '+student.track+ '</p>';
            message+= '<p>Points: '+student.points+  '</p>';
            message+= '<p>Achievements: '+student.achievements+ '</p>';
            print(message)
        }
    }
 }
 while(search!=='quit');

I guess you already declared students arrray in your code.

Gil Huss
Gil Huss
2,750 Points

as Ashu said, use {} for the do/while function. and "student" is declared but "students" isn't (or it might not be included in your post) same as "message".

Next thing I'm not as sure about, but i think your var "search" should be outside of the do/while function, so the while can grab it too. Please correct me if I'm wrong.

Sandra Vu
Sandra Vu
3,525 Points

Thanks everyone. You are so helpful! :D