Monthly Archives: July 2012

See if you can guess what the following Javascript code does.

for(var i=0; i < 5; i++) { var b = document.createElement("input"); b.type = "button"; b.onclick = function() { alert(i); } document.appendChild(b); }

If you guessed, "It will create five buttons which, when clicked, pop up an alert with the numbers 0, 1, 2, 3, and 4," then you're mistaken.  Javascript passes only the reference of 'i' to the b.onclick.  Instead, you'll see '5' displayed five times.  This has plagued me for the past two days, and I've yet to find an elegant solution.

1 cup butter
2 cups white sugar
2 eggs
1 1/2 cup unsweetened cocoa powder (or 4x 1 oz squares dark chocolate)
1 cup all-purpose flour
1/4 – 1/2 teaspoon salt
1/4 teaspoon baking powder (don’t normally use this)
Remainder of the dark chocolate bar above
10 oz raspberry jam
1 package raspberries

Mix the butter, sugar, and eggs in one bowl. Mix the flour, cocoa powder, and salt in another. Mix them together in a large bowl and add chunks of the dark chocolate. Grease a pan (9×9, according to recipe, but I make double, so I use a 9×11). Dump just over half the batter into the pan. Add 10 oz of the raspberry jam into the batter now and stir. Add the raspberries. Add it on top of the chocolate layer.

Bake for 325 degrees F for 40 minutes. The jelly usually makes the top look a little underdone. The recipe normally calls for 350 F and 35 minutes, but I’ve found that you need to cook slightly longer because of the raspberry preserves.