Skip to content

Instantly share code, notes, and snippets.

@xavi-
Forked from jimbojw/gist:583389
Created October 18, 2010 21:21
Show Gist options
  • Select an option

  • Save xavi-/633087 to your computer and use it in GitHub Desktop.

Select an option

Save xavi-/633087 to your computer and use it in GitHub Desktop.
Interview gist -- Can candidate read, debug, and fix code?
// For each of the following code fragments:
// a. what does the code do?
// b. what did the author intend for it to do?
// c. how would you fix it?
// NOTE: all code samples work exactly the same in all browsers
// 1. object literals
var data = [ { high: 100, low: 81 }, { high: 93, low: 73 }, { high: 60, low: 32 } ];
function getAverages(data) {
var avgs = [];
for(var i = 0; i < avgs.length; i++) {
avgs.push(data[i].high + data[i].low / 2);
}
return avgs;
}
getAverages(data); // returns [];
// 2. list of links
var p = document.createElement('p');
for (var i=0; i<10; i++) {
var a = document.createElement('a');
a.innerHTML = "link " + i;
a.onclick = function() {
alert("you clicked link " + i);
return false;
};
p.appendChild(a);
}
document.body.appendChild(p);
// 3. method callback
var bob = {
firstname: "Bob",
greet: function() {
alert("Hi, I'm " + this.firstname);
}
}
window.onload = bob.greet;
@aDemoUzer

Copy link
Copy Markdown

i anzwered theze. I can haz job now?
BTW, // 2. method callback should be 3.

@xavi-

xavi- commented Jan 21, 2011

Copy link
Copy Markdown
Author

Haha, you're right. Good catch.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment