Created
October 4, 2013 07:31
-
-
Save TBD/6822256 to your computer and use it in GitHub Desktop.
solution for http://toys.usvsth3m.com/javascript-under-pressure/ - ugly code
This file contains hidden or 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
function doubleInteger(i) { | |
// i will be an integer. Double it and return it. | |
return i*2; | |
} | |
function isNumberEven(i) { | |
// i will be an integer. Return true if it's even, and false if it isn't. | |
return i%2==0 | |
} | |
function getFileExtension(i) { | |
// i will be a string, but it may not have a file extension. | |
// return the file extension (with no period) if it has one, otherwise false | |
var ext = i.split('.').pop() | |
return ext==i?false:ext | |
} | |
function longestString(i) { | |
// i will be an array. | |
// return the longest string in the array | |
var l = 0; | |
var max = 0 | |
for (var x = 0; x < i.length; x++) { | |
if (typeof(i[x])=='string') | |
if (i[x].length > max) { | |
l = x; | |
max = i[x].length | |
} | |
} | |
return i[l]; | |
} | |
function arraySum(i) { | |
// i will be an array, containing integers, strings and/or arrays like itself. | |
// Sum all the integers you find, anywhere in the nest of arrays. | |
function flatten(array){ | |
var flat = []; | |
for (var i = 0, l = array.length; i < l; i++){ | |
var type = Object.prototype.toString.call(array[i]).split(' ').pop().split(']').shift().toLowerCase(); | |
if (type=='array') flat = flat.concat(flatten(array[i])) | |
if (type=='number') flat.push(array[i]) | |
} | |
return flat; | |
} | |
return flatten(i).reduce(function(a,b){return a+b}) | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment