Skip to content

Instantly share code, notes, and snippets.

@stecb
Created October 4, 2013 09:49
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save stecb/6823560 to your computer and use it in GitHub Desktop.
Save stecb/6823560 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
return i.split('.')[1] || false;
}
function longestString(i) {
// i will be an array.
// return the longest string in the array
var str = '', j = 0;
for(; j < i.length; j++) {
if (typeof i[j] === 'string' && i[j].length > str.length) {
str = i[j];
}
}
return str;
}
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, j = 0;
for(; j < i.length; j++) {
if(typeof i[j] === 'number') {
sum += i[j];
} else {
sum += Object.prototype.toString.call(i[j]) === '[object Array]' ? arraySum(i[j]) : 0;
}
}
return sum;
}
@stecb
Copy link
Author

stecb commented Oct 4, 2013

Btw, u can't argue mine is more readable :P

@DimitarChristoff
Copy link

also. getFileExtension("mylib.min.js") fail.

@stecb
Copy link
Author

stecb commented Oct 4, 2013

Under pressure, my OS allows just one .

@DimitarChristoff
Copy link

in 86 chars, fixes most usecases for file extension:

function f(n,d,r){return r=!1,d='.',n.indexOf(d)!==-1&&(r=n.split(d).pop()),r}

fails on things like f('foo.'); or f('i like triangles. and model trains');

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment