Unit 5 Lesson 13 puzzle 15 behavior of console.log

Unit 5 lesson 13 puzzle 15 has us do something like this:

var myArray = [10, 20, 30];
myArray[1000] = 40;
console.log(myArray);

Looking at the console, App Lab gives us:
ERROR: Line: 3: TypeError: Cannot read property 'isPrimitive' of undefined
If we try this in any other implementation of Javascript we get:
[ 10, 20, 30, <997 empty items>, 40 ]
The latter seems more helpful to our kids. They can see how Javascript arrays work differently than most other languages.

Because enumeration of sparse arrays is broken console.log() will not work correctly, but notice it also breaks map(), JSON.stringify(), etc. etc. etc. Basic language functionality our advanced kids might want to try out is broken.

The work around for right now is to write code like this:

for(var n in myArray) {
  console.log(n + ":" + myArray[n]);
}
2 Likes

Thanks for pointing this out. This looks like a bug with how App Lab handles arrays. I’ve made a note with our team about this, and hopefully we will be able to fix it soon. Until then, the workaround you posted is the right approach.

Thanks
Anjali
Code.org Engineering