JavaScript AJAX Basics (retiring) Programming AJAX Processing JSON Data

Olivia Posch
Olivia Posch
Front End Web Development Techdegree Student 9,534 Points

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
171,342 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
Akash Sharma
Akash Sharma
Full Stack JavaScript Techdegree Student 14,147 Points

Steven Parker is there anything in the console or ide that could help with an error say like non-terminating loop pending before execution?

Steven Parker
Steven Parker
171,342 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.