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 Build an Object Challenge, Part 2 Solution

I'm really stuck in this endless loop. Any feedback will be welcome!!!

var students = [ { name: 'Dave', track: 'Front End Development', achievements: 158, points: 14730 }, { name: 'Jody', track: 'iOS Development with Swift', achievements: '175', points: '16375' }, { name: 'Jordan', track: 'PHP Development', achievements: '55', points: '2025' }, { name: 'John', track: 'Learn WordPress', achievements: '40', points: '1950' }, { name: 'Trish', track: 'Rails Development', achievements: '5', points: '350' } ];

var message; var result="" ; var studentList=""; var search="";

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

while(true){ search = prompt('Search for a student. Type quit if you want to leave'); if (search === 'quit') { break; } for (var i = 0; i < studentList.length; i+=1) { students = studentlList[i]; if(search === students.name){ result += "<p>Student: " + students[i].name + "</p>"; result += "<p>Track: " + students[i].track + "</p>"; result += "<p>Points: " + students[i].points + "</p>"; result += "<p>Achievements: " + students[i].achievements + "</p>"; message = result; print(message); } else { print(search + ' not in the list'); break; } }

}

Zachary Kaufman
Zachary Kaufman
1,463 Points

I'll look for the code real quick but for future reference when you are putting code in the treehouse community start it with three followed by the word "javascript" and end with three more :)

var students = [ 
{ name: 'Dave', track: 'Front End Development', achievements: 158, points: 14730 }, 
{ name: 'Jody', track: 'iOS Development with Swift', achievements: '175', points: '16375' }, 
{ name: 'Jordan', track: 'PHP Development', achievements: '55', points: '2025' }, 
{ name: 'John', track: 'Learn WordPress', achievements: '40', points: '1950' }, 
{ name: 'Trish', track: 'Rails Development', achievements: '5', points: '350' } ];

var message; 
var result="" ; 
var studentList=""; 
var search="";

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

while(true) { 
  search = prompt('Search for a student. Type quit if you want to leave'); 
  if (search === 'quit') { 
    break; 
  } 
  for (var i = 0; i < studentList.length; i+=1) { 
    students = studentlList[i]; 
    if(search === students.name) { 
      result += "<p>Student: " + students[i].name + "</p>"; 
      result += "<p>Track: " + students[i].track + "</p>"; 
      result += "<p>Points: " + students[i].points + "</p>"; 
      result += "<p>Achievements: " + students[i].achievements + "</p>"; message = result; print(message); 
    } else { 
      print(search + ' not in the list'); break; 
    } 
  }

}
Aman Mundra
Aman Mundra
2,755 Points

The while loop does terminate when you type in 'quit'.

However, your for loop has an odd end bound - '''studentList''' is defined as a blank, empty String. As such it has a length = 0. So when you terminate your loop with '''i < studentList.length''', you are basically writing '''i < 0''', which implies the for loop never runs.