Last active
July 7, 2017 10:33
-
-
Save lightsuner/9544434 to your computer and use it in GitHub Desktop.
http://games.usvsth3m.com/javascript-under-pressure/ - my solutions
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
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 | |
if (i.indexOf('.') === -1) { | |
return false; | |
} | |
return i.split('.').pop(); | |
} | |
function longestString(i) { | |
// i will be an array. | |
// return the longest string in the array | |
return i.reduce(function(prev, curr) { | |
var isPrevString = (typeof prev == 'string'); | |
var isCurrString = (typeof curr == 'string'); | |
if (!isPrevString || !isCurrString) { | |
if (!isPrevString && !isCurrString) { | |
return ''; | |
} else { | |
return isPrevString? prev: curr; | |
} | |
} | |
return (prev.length > curr.length) ? prev : curr; | |
}); | |
} | |
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. | |
switch (typeof i) { | |
case 'object': | |
return i.reduce(function(prev, curr){ | |
return prev + arraySum(curr); | |
}, 0); | |
case 'number': | |
return i; | |
default: | |
return 0; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
daf da dfa