Last active
December 25, 2015 06:49
-
-
Save stedman/6934952 to your computer and use it in GitHub Desktop.
My answers for the http://toys.usvsth3m.com/javascript-under-pressure/ challenge. Please question/comment/improve!!
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
// 1 | |
function doubleInteger(i) { | |
// i will be an integer. Double it and return it. | |
return i*2; | |
} | |
// 2 | |
function isNumberEven(i) { | |
// i will be an integer. Return true if it's even, and false if it isn't. | |
return !(i%2) | |
} | |
// 3 | |
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 | |
return (i.match(/\.(.+)$/)||[,false])[1]; | |
// for an explanation of this very useful snippet, | |
// see http://james.padolsey.com/javascript/match-trick/ | |
} | |
// 4 | |
function longestString(i) { | |
// i will be an array. | |
// return the longest string in the array | |
return i.sort(function(a,b){ | |
if (typeof a != 'string') return; | |
return a.length > b.length; | |
}).pop(); | |
} | |
// 5 | |
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. | |
var sum = 0; | |
i.forEach(function(j) { | |
if (typeof j == 'number') { | |
sum += j; | |
} else if (Array.isArray(j)) { | |
sum += arraySum(j); | |
} | |
}); | |
return sum; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment