I found this question on interviewcake.com.
<button id="btn-0">Button 1!</button>
<button id="btn-1">Button 2!</button>
<button id="btn-2">Button 3!</button>
<script type="text/javascript">
var prizes = ['A Unicorn!', 'A Hug!', 'Fresh Laundry!'];
for (var btnNum = 0; btnNum < prizes.length; btnNum++) {
document.getElementById('btn-' + btnNum).onclick = function() {
alert(prizes[btnNum]);
};
}
</script>
on click of any button it alerts 'undefined'. Can't figure out this unexpected behavior. Your help is highly appreciated.