Skip to content

Instantly share code, notes, and snippets.

@speedyGonzales
Created February 20, 2015 15:08
Show Gist options
  • Save speedyGonzales/5a2f09c321cb5c0b5232 to your computer and use it in GitHub Desktop.
Save speedyGonzales/5a2f09c321cb5c0b5232 to your computer and use it in GitHub Desktop.
javascript under pressure
//01. i will be an integer. Double it and return it.
function doubleInteger(i) {
return i*2;
}
//02. i will be an integer. Return true if it's even, and false if it isn't.
function isNumberEven(i) {
return (i%2 == 0);
}
//03. 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
function getFileExtension(i) {
if(i.indexOf('.')==-1){
return false
}else{
return i.split('.')[1];
}
}
//04. i will be an array. return the longest string in the array
function longestString(i) {
var arr=Array();
for(var j=0;j<i.length;j++){
if(typeof i[j] ==='string'){
arr.push(i[j]);
}
}
return arr.sort(function (a, b) { return b.length - a.length; })[0];
}
//05 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.
function arraySum(i) {
sum=0;
for(var j=0;j<i.length;j++){
if(typeof i[j]==='number'){
sum+=parseInt(i[j]);
}else{
if(Array.isArray(i[j])){
sum+=parseInt(arraySum(i[j]));
}
}
}
return sum;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment