Skip to content

Instantly share code, notes, and snippets.

@LeaVerou
Last active December 25, 2015 06:49
Show Gist options
  • Save LeaVerou/6934569 to your computer and use it in GitHub Desktop.
Save LeaVerou/6934569 to your computer and use it in GitHub Desktop.
// DISCLAIMER: This is not necessarily good code. It’s just code that I wrote
// as quickly as possible to do each task.
// 1
return 2*i;
// 2
return !(i%2);
// 3
return (i.match(/\.(.+)$/) || [,false])[1];
// 4
var l = '';
for (var n=0; n<i.length; n++) {
if (typeof i[n] == 'string' && i[n].length > l.length) {
l = i[n];
}
}
return l;
// 5
function arraySum(a) {
var sum = 0;
for (var i=0; i<a.length; i++) {
var n = a[i];
if (n === +n) {
sum += n;
}
else if (Array.isArray(n)) {
sum += arraySum(n);
}
}
return sum;
}
@iamJoeTaylor
Copy link

Love the if (n === +n) { trick. Simple & clean

@eAmin
Copy link

eAmin commented Dec 19, 2013

@iamJoeTaylor: What does that mean?

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