Created
June 26, 2012 01:29
-
-
Save caseyohara/2992547 to your computer and use it in GitHub Desktop.
rmurphey/js-assessment answers
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
if (typeof define !== 'function') { var define = require('amdefine')(module); } | |
define(function() { | |
return { | |
indexOf : function(arr, item) { | |
for (var i = 0, total = arr.length; i < total; i++) { | |
if (arr[i] == item) | |
return i; | |
} | |
return -1; | |
}, | |
sum : function(arr) { | |
return arr.reduce(function(prev, current){ | |
return prev + current; | |
}); | |
}, | |
remove : function(arr, item) { | |
for (var i = 0, total = arr.length; i < total; i++) { | |
if (arr[i] == item) { | |
arr.splice(i, 1); | |
i--; | |
} | |
} | |
return arr; | |
}, | |
append : function(arr, item) { | |
arr.push(item); | |
return arr; | |
}, | |
truncate : function(arr) { | |
arr.pop(); | |
return arr; | |
}, | |
concat : function(arr1, arr2) { | |
for (var i = 0, total = arr2.length; i < total; i++) { | |
arr1.push(arr2[i]); | |
} | |
return arr1; | |
}, | |
insert : function(arr, item, index) { | |
arr.splice(index, 0, item); | |
return arr; | |
}, | |
count : function(arr, item) { | |
return arr.filter(function(el){ | |
return el === item; | |
}).length; | |
}, | |
duplicates : function(arr) { | |
var known = []; | |
var duplicates = []; | |
for (var i = 0, total = arr.length; i < total; i++) { | |
if (known.indexOf(arr[i]) >= 0) { | |
duplicates.push(arr[i]) | |
} | |
else { | |
known.push(arr[i]); | |
} | |
} | |
return duplicates; | |
}, | |
square : function(arr) { | |
return arr.map(function(i){ | |
return Math.pow(i,2); | |
}); | |
}, | |
findAllOccurrences : function(arr, target) { | |
var occurences = []; | |
for (var i = 0, total = arr.length; i < total; i++) { | |
if(arr[i] == target) | |
occurences.push(i); | |
} | |
return occurences; | |
} | |
}; | |
}); |
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
define(function() { | |
return { | |
async : function() { | |
return { | |
then: function(callback){ | |
callback(true); | |
} | |
} | |
}, | |
manipulateRemoteData : function(url) { | |
var request = new XMLHttpRequest(); | |
return { | |
then: function(callback) { | |
request.onreadystatechange = function(){ | |
if (request.readyState == 4 && request.status >= 200 && request.status < 400) { | |
var result = JSON.parse(request.responseText); | |
var names = result["people"].map(function(item){ | |
return item["name"] | |
}).sort(); | |
return callback(names); | |
} | |
} | |
request.open("GET", url, true); | |
request.send(); | |
} | |
} | |
} | |
}; | |
}); |
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
if (typeof define !== 'function') { var define = require('amdefine')(module); } | |
define([ 'use!underscore' ], function(_) { | |
return { | |
fizzBuzz : function(num) { | |
if (num == null) return false; | |
var str = ""; | |
if (!(num % 3)) str += 'fizz'; | |
if (!(num % 5)) str += 'buzz'; | |
if (!str) return num; | |
return str; | |
}, | |
or : function(a, b) { | |
return a || b; | |
}, | |
and : function(a, b) { | |
return a && b; | |
} | |
}; | |
}); |
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
if (typeof define !== 'function') { var define = require('amdefine')(module); } | |
define(function() { | |
return { | |
argsAsArray : function(fn, arr) { | |
return fn.apply(fn, arr); | |
}, | |
speak : function(fn, obj) { | |
return fn.call(obj); | |
}, | |
functionFunction : function(str) { | |
return function(str2){ | |
return str + ", " + str2; | |
} | |
}, | |
partial : function(fn, str1, str2) { | |
var str3 = str1 + ", " + str2; | |
return function(str4){ | |
return str3 + str4; | |
} | |
}, | |
useArguments : function() { | |
var args = []; | |
for (var i in arguments) { | |
args.push(arguments[i]) | |
} | |
return args.reduce(function(prev, current){ | |
return prev + current; | |
}); | |
}, | |
callIt : function(fn) { | |
return fn.apply(fn, Array.prototype.slice.call(arguments, 1)); | |
}, | |
curryIt : function(fn) { | |
var args = Array.prototype.slice.call(arguments, 1); | |
return function(){ | |
var _args = args.concat(Array.prototype.slice.call(arguments)); | |
return fn.apply(null, _args); | |
} | |
}, | |
makeClosures : function(arr, fn) { | |
return arr.map(function(item){ | |
return (function(item){ | |
return function(){ | |
return fn(item); | |
} | |
})(item); | |
}); | |
} | |
}; | |
}); |
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
if (typeof define !== 'function') { var define = require('amdefine')(module); } | |
define(function() { | |
return { | |
createModule : function(str1, str2) { | |
return { | |
name: str1, | |
greeting: str2, | |
sayIt : function(){ | |
return "" + this.name + ", matt"; | |
} | |
} | |
} | |
}; | |
}); |
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
if (typeof define !== 'function') { var define = require('amdefine')(module); } | |
define(function() { | |
return { | |
alterContext : function(fn, obj) { | |
return fn.call(obj); | |
}, | |
alterObjects : function(constructor, greeting) { | |
constructor.prototype.greeting = greeting; | |
}, | |
iterate : function(obj) { | |
var props = []; | |
for (var prop in obj) { | |
if (obj.hasOwnProperty(prop)) | |
props.push("" + prop + ": " + obj[prop]); | |
} | |
return props; | |
} | |
}; | |
}); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment