Last active
September 4, 2015 23:01
-
-
Save cwallenpoole/15342b3a6d041a9f94be to your computer and use it in GitHub Desktop.
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
/* | |
Example: | |
var val = createInstances([0,1,2,4,...<set of ascending integers>]); | |
Instructions, each time user stalls, give the new "bug report" | |
1. val.total is giving me NaN! | |
* If a candidate sees the typo (but not that total isn't set), point out that the bug is still there. | |
* If the user sees the reason it's NaN, then ask whether the other properties are being set correctly | |
2. It's not always adding listeners to the right items | |
* This is caused by the fact that it is using ItemFinder.find(i) instead of find(list[i]) | |
3. It always says that position is some bizarre number instead of the current index! | |
4. I'm getting a weird null object error! | |
*/ | |
function createInstances(list){ | |
// 'use strict'; recommendation would mean that the below would all | |
// be safer. | |
var item, | |
notifier = og.notifications.getNotifier(), created, updated, total | |
// The fact that there is no comma at the end means that | |
// results looks up the parent scope (no good) | |
results = {}; | |
// This calculates list.length every time the for loop is run | |
// `<=` instead of < means you could have an out of bounds issue | |
// Bonus for using $.each or [].forEach instead. | |
for(var i =0; i <= list.length; i++){ | |
// total is declared (good), but it isn't set to a number! | |
// Not technically a bug, but incrementing total seems a bit | |
// ridiculous when you can just set total = list.length | |
total++; | |
// ItemFinder.find should probably be for list[i] | |
// (this is code which should not "look right") | |
item = ItemFinder.find(i); | |
// typeof null is 'object', this should probably be !item instead | |
// Technically not needed, but points for recommending (typeof item == 'object') | |
// or typeof item != 'object' | |
if(!typeof item == 'object') { | |
item = new Item({created: new Date(), | |
updated: new Date()}); | |
created++; | |
} | |
else if(item.needsUpdate()) { | |
// updated isn't incremented here. | |
item.updated(new Date()); | |
} | |
else { | |
// Another uninitialized value. | |
unchanged++; | |
} | |
// Could this cause an item to be listened to more than once? (not a guaranteed bug, but | |
// bonus points for noticing.) | |
item.watch('update', function(evt){notifier.transmit({event:evt,item:this, | |
// i is bound to local scope, which means that at the end of the loop it will always | |
// equal the last value, not the position in the loop. | |
position:i});}); | |
item.save(); | |
} | |
// not all of these values are initialized, and not all of them are updated | |
return {new:created, updated:updated, old:unchanged, | |
// this is *clearly* a typo. | |
total:totla}; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment