Created
June 26, 2012 02:08
-
-
Save stevenmaguire/2992746 to your computer and use it in GitHub Desktop.
js-assessment responses
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) { | |
return arr.indexOf(item) | |
}, | |
sum : function(arr) { | |
var total = 0; | |
for (var i in arr) { | |
if (typeof(arr[i]) == 'number') total += arr[i] | |
} | |
return total | |
}, | |
remove : function(arr, item) { | |
var index = arr.indexOf(item) | |
arr.splice(index,1) | |
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 in arr2) { | |
arr1.push(arr2[i]) | |
} | |
return arr1 | |
}, | |
insert : function(arr, item, index) { | |
arr.splice(index,0,item) | |
return arr | |
}, | |
count : function(arr, item) { | |
var total = 0; | |
for (var i in arr) { | |
if (arr[i] === item) total++ | |
} | |
return total | |
}, | |
duplicates : function(arr) { | |
var sorted_arr = arr.sort(); | |
var results = []; | |
for (var i = 0; i < arr.length - 1; i++) { | |
if (sorted_arr[i + 1] == sorted_arr[i]) results.push(sorted_arr[i]) | |
} | |
return results | |
}, | |
square : function(arr) { | |
for (var i in arr) { | |
if (typeof(arr[i]) == 'number') arr[i] = arr[i]*arr[i] | |
} | |
return arr | |
}, | |
findAllOccurrences : function(arr, target) { | |
var results = []; | |
for (var i in arr) { | |
if (arr[i] === target) results.push(i) | |
} | |
return results | |
} | |
}; | |
}); |
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 { | |
manipulateRemoteData : function(url) { | |
var get = $.ajax({ | |
'url':url, | |
'error':function(){ | |
console.log('poop on a stick') | |
} | |
}) | |
return { then : function(callback){ | |
get.done(function(results){ | |
var peopleArray = new Array() | |
$.each(results.people,function(k,person){ | |
peopleArray.push(person.name) | |
}) | |
return callback(peopleArray.sort()) | |
}) | |
} | |
} | |
}, | |
async : function() { | |
var sleepytime = $.Deferred() | |
var timeoutID = window.setTimeout(function(){ sleepytime.resolve(true) }, 1000) | |
return sleepytime.promise() | |
} | |
}; | |
}); |
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) { | |
// write a function that receives a number as its argument; | |
// if the number is divisible by 3, the function should return 'fizz'; | |
// if the number is divisible by 5, the function should return 'buzz'; | |
// if the number is divisible the 3 and 5, the function should return | |
// 'fizzbuzz'; | |
// otherwise the function should return the number | |
var dope = '' | |
if (num % 3 == 0) dope += 'fizz' | |
if (num % 5 == 0) dope += 'buzz' | |
if (dope != '') return dope | |
else return num | |
}, | |
or : function(a, b) { | |
if (a || b) return true | |
else return false | |
}, | |
and : function(a, b) { | |
if (a && b) return true | |
else return false | |
} | |
}; | |
}); |
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(this, arr) | |
}, | |
speak : function(fn, obj) { | |
return fn.call(obj) | |
}, | |
functionFunction : function(str1) { | |
var concatFn = function(str2) { | |
return str1 + ', ' + str2 | |
} | |
return concatFn | |
}, | |
partial : function(fn, str1, str2) { | |
var partialFn = function(str3) { | |
return fn(str1, str2, str3) | |
} | |
return partialFn | |
}, | |
useArguments : function() { | |
var args = Array.prototype.slice.call(arguments),total=0 | |
for (var i in args){ | |
if (typeof(args[i]) === 'number') total += args[i]*1 | |
} | |
return total | |
}, | |
callIt : function(fn) { | |
var args = Array.prototype.slice.call(arguments),vars = new Array() | |
for (var i in args){ | |
if (typeof(args[i]) === 'number') vars.push(args[i]*1) | |
} | |
fn.apply(fn,vars) | |
}, | |
curryIt : function(fn) { | |
var args = Array.prototype.slice.call(arguments, 1) | |
return function(){ | |
var deep = args.concat(Array.prototype.slice.call(arguments)) | |
return fn.apply(null, deep) | |
} | |
}, | |
makeClosures : function(arr, fn) { | |
return arr.map(function(thang){ | |
return (function(thang){ | |
return function(){ | |
return fn(thang) | |
} | |
})(thang) | |
}) | |
} | |
}; | |
}); |
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) { | |
var lord = (function () { | |
var bolton = {} | |
bolton.name = str2 | |
bolton.greeting = str1 | |
bolton.sayIt = function () { | |
return bolton.greeting+', '+bolton.name | |
} | |
return bolton | |
}()) | |
return lord | |
} | |
}; | |
}); |
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 ownProperties = [] | |
for (var prop in obj) { | |
if (obj.hasOwnProperty(prop)) { | |
ownProperties.push(prop + ': ' + obj[prop]) | |
} | |
} | |
return ownProperties | |
} | |
}; | |
}); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment