Skip to content

Instantly share code, notes, and snippets.

@abhishekdev
Last active December 24, 2015 21:39
Show Gist options
  • Save abhishekdev/6866955 to your computer and use it in GitHub Desktop.
Save abhishekdev/6866955 to your computer and use it in GitHub Desktop.
Solutions to questions at http://toys.usvsth3m.com/javascript-under-pressure/. I can JavaScript under pressure in 4 min 55 secs
/**
* Solutions to questions at {@link http://toys.usvsth3m.com/javascript-under-pressure/ }
*/
// Ans 1
function doubleInteger(i){
return i*2;
}
// Ans 2
function isNumberEven(i){
return i%2 == 0;
}
// Ans 3
function getFileExtension(i){
var rawExt = i.match(/\.+[a-zA-Z0-9]+/),
ext = false;
if(rawExt && rawExt.length){
ext = rawExt[0].substring(1);
}
return ext;
}
// Ans 4
function longestString(i){
for(var x = i.length - 1; x>=0; --x){
if(typeof i[x] != 'string'){
i[x] = "";
}
}
i.sort(function(a,b){
return b.length - a.length;
});
return i[0];
}
// Ans 5
function arraySum(i){
var sum=0,
x = i.length - 1,
val;
for(; x>=0; --x){
val = i[x];
switch(typeof val){
case "number" : sum += val; break;
case "object" : sum += arraySum(val); break;
}
}
return sum;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment