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

I have no idea why the WHILE LOOP always end. Please help! Thank you very much!

var students = [ { name : "shironeko" , track : "Ruby on Rails" , archi : 100 , points : 14000 , }, { name : "chibi bang" , track : "JS fullstack" , archi : 200 , points : 16000 , }, { name : "kuro" , track : "Django" , archi : 80 , points : 10000 , }, { name : "chibi side" , track : "Python" , archi : 48 , points : 50000 , }, { name : "kuro" , track : "Wordpress" , archi : 60 , points : 15000 , }, { name : "nora" , track : "Java" , archi : 800 , points : 465000 , } ];

var student; var student_name; var html; var user_search;

function print(mess) { document.write(mess); }

while (true) { user_search = prompt("Type a name of student or type 'quit' to exit..."); user_search = user_search.toLowerCase(); if (user_search === "quit" || user_search === null) { } for (var i=0 ; i <= students.length ; i += 1) { student = students[i]; student_name = student.name; if (student_name === user_search) { html = "<h2>Student name: " + student_name + "</h2><br>"; print(html); html = "Track: " + student.track + "<br>"; print(html); html = "Archi: " + student.archi + "<br>"; print(html); html = "Points: " + student.points + "<br><hr>"; print(html); } } }

2 Answers

Marco Amadio
Marco Amadio
4,882 Points

Hi! In your for loop you set the condition to i <= students.length, but should be i < students.length. That's because array indexing starts from 0, but length property returns the number of array's elements.

e.g.

var array = ["foo", "bar", "lorem", "ipsum"];

array.length /* will return 4, because array has 4 elements */
/* But the array has indexing will be: */
array[0] /* returns "foo" */
array[1] /* returns "bar" */
array[2] /* returns "lorem" */
array[3] /* returns "ipsum" */

So, your for loop will execute when i will be 6, because your array has 6 elements, but students[6] will return undefined. Then your code try to get the value with key "name" from undefined and will throw this error: Uncaught TypeError: Cannot read property 'name' of undefined That's why your while loop end.

Hope it helps.

These days since everything IE9 and up supports forEachI prefer to use that since it follows the functional programming paradigm.

Note I'm using ES6 syntax for the arrow function, since it's fairly common in production these days and is being adopted by browsers

listOfItems.forEach(item=> {
      if (item.type==='alert')
        // do something
      else
       // do something else
}