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 Multiple Items with Arrays Using For Loops with Arrays

Leno Marin
PLUS
Leno Marin
Courses Plus Student 1,323 Points

Getting an "Allocation size overflow" error in Firefox Dev Console

Here is the code: var playList = [ 'I Did It My Way', 'Respect', 'Imagine', 'Born to Run', 'Louie Louie', 'Maybellene' ];

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

function printList(list){ var listHTML = '<ol>'; for( var i= 0; i < list.length; i +=i){ listHTML += '<li>' + list[i] + '</li>'; } listHTML += '</ol>'; print(listHTML); }

printList(playList);

2 Answers

I'm not sure what your original formatting is, as you didn't use the code markdown in your question, so I can't point you to the root of the problem just by looking at it. When I copy/pasted your code into CodePen, I forgot to turn off the setting for auto-updating the preview and Chrome ran out of memory after a few seconds. However, once I regained control, I turned off the auto-update and pasted the code in.

After I applied a "Tidy JS" from the drop down I became aware of your i +=i inside the for-loop and it became pretty apparent that was your issue. I'm not sure what you intended to do with it, but essentially what was happening on the first pass was a 0 += 0 statement that essentially locked the loop into an infinite condition and never incremented. Supposing you started the for-loop condition with var i = 1 instead, you would only print indices 1, 3, and 5, which didn't seem like what you wanted to do, so I changed i +=i to i++ and it prints off the entirety of the playList, as I expect you wanted. Here's my slightly cleaned up version of your code:

var playList = ['I Did It My Way', 'Respect', 'Imagine', 'Born to Run', 'Louie Louie', 'Maybellene'];

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

function printList(list) {
  var listHTML = '<ol>';
  for (var i = 0; i < list.length; i++) {
    listHTML += '<li>' + list[i] + '</li>';
  }
  listHTML += '</ol>';
  print(listHTML);
}

printList(playList);

As a side-note, I would recommend steering away from document.write() unless absolutely necessary. It's sort of bad juju in most situations. You might check out things like document.createElement() (link) and element.appendChild() (link), or other various ways to get the results you're after.

Anyway, good luck :)

Leno Marin
PLUS
Leno Marin
Courses Plus Student 1,323 Points

Ahh! Mike, that was it! I had spent over an hour trying to figure out what was going wrong. Thanks.