-
-
Save xavi-/633087 to your computer and use it in GitHub Desktop.
Interview gist -- Can candidate read, debug, and fix code?
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// 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; |
Haha, you're right. Good catch.
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
i anzwered theze. I can haz job now?
BTW, // 2. method callback should be 3.