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 AJAX Basics (retiring) Programming AJAX Processing JSON Data

i don't know what is wrong with my code on this exemplo .

var xhr = new XMLHttpRequest(); xhr.onreadystatechange = function (){ if (xhr.readyState === 4) { var employees = JSON.parse(xhr.responseText); var statusHTML = '<lu class="bulleted">'; for (var i = 0; i < employees.length; i =+ 1) { if (employees[i].inoffice === true){ statusHTML += '<li class="in">'; }else{ statusHTML += '<li class="out">'; } statusHTML += employees[i].name; //the error is here: Uncaught RangeError: Invalid string length statusHTML += '</li>'; } statusHTML += '</ul>'; document.getElementById('employeeList').innerHTML = statusHTML; } }; xhr.open('GET', 'data/employees.json'); xhr.send();

3 Answers

Steven Parker
Steven Parker
231,269 Points

It looks like your loop keeps adding on to the "statusHTML" variable until it exceeds the maximum length. Your "for" statement has a couple of characters transposed which makes the loop go on forever (or until error):

    for (var i = 0; i < employees.length; i = +1) {  // you probably want "i += 1" instead here

I need a new glasses :-)

Steven Parker
Steven Parker
231,269 Points

Something like a "non-terminating loop" might not be what you intended, but it's not actually a program error from the viewpoint of the language itself.

I believe some IDE's have a facility to detect and interrupt a program that is looping very quickly, but I don't think the workspace does. But your browser may have debugging features that allow you to manually interrupt a program and to set breakpoints to occur when things (like too many iterations) occur.