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

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