Skip to content

Instantly share code, notes, and snippets.

@antife-yinyue
Last active December 25, 2015 03:59
Show Gist options
  • Save antife-yinyue/6913502 to your computer and use it in GitHub Desktop.
Save antife-yinyue/6913502 to your computer and use it in GitHub Desktop.
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 m = i.match(/^.*\.([a-z]+)$/)
return !!m && m[1]
}
function longestString(i) {
// i will be an array.
// return the longest string in the array
return i.filter(function(item) {
return typeof item === 'string'
}).sort(function(x, y) {
return x.length > y.length
}).pop()
}
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 ret = [], flatten
(flatten = function(arr) {
arr.forEach(function(item) {
if (Array.isArray(item)) {
flatten(item)
}
else if (typeof item === 'number') {
ret.push(item)
}
})
})(i)
return ret.reduce(function(memo, item) {
return memo + item
})
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment