Skip to content

Instantly share code, notes, and snippets.

@lylijincheng
Last active December 24, 2015 23:29
Show Gist options
  • Save lylijincheng/6880620 to your computer and use it in GitHub Desktop.
Save lylijincheng/6880620 to your computer and use it in GitHub Desktop.
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 index, entry, ret = 0, len = i.length;
for(index = 0; index < len; index += 1){
entry = i[index];
ret += entry instanceof Array ? arraySum(entry)) : entry;
}
return ret;
}
function doubleInteger(i) {
// i will be an integer. Double it and return it.
i *= 2;
return i;
}
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 lastIndex = i.lastIndexOf('.');
return lastIndex > -1 ? i.slice(lastIndex + 1) : false;
}
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 longestString(i) {
// i will be an array.
// return the longest string in the array
var index, length, current, longestEntry;
for (index = 0, length = i.length; index < length; index += 1) {
current = i[index];
if (typeof current !== 'string') {
continue;
} else if (current.length > (longestEntry = longestEntry || current).length) {
longestEntry = current;
}
}
return longestEntry;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment