A quiz about preloading images and for loops

24 Jan 2017

It’s been about a year since the last quiz. Please direct yourself to the nearest whiteboard.

Given the following block of JS from Bug 1328542 (don’t look yet, cheaters. Ugh), how many new Images are preloaded?

function FP_preloadImgs() {//v1.0
 var d=document,a=arguments;
 if(!d.FP_imgs) d.FP_imgs=new Array();
 for(var i=0; i<a.length; i  ) {
   d.FP_imgs[i]=new Image;
   d.FP_imgs[i].src=a[i];
 }
}

FP_preloadImgs(/*url*/'images/button6.jpg',/*url*/'images/button5.jpg');

. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .

If you guessed “as many as it takes until the browser hits a OOM (out of money) or crashes”, congratulations. To bad you just spent all your browser money, you should probably buy that FP_preloadImgs v2.0 upgrade license.

The good news is that the DIY patch is much cheaper: just be sure to increment your, uh, expression thingies (i in this case) in for loops when pre-loading images (and generally everywhere else).