Skip to content

Instantly share code, notes, and snippets.

@iSkore
Created April 21, 2016 18:37
Show Gist options
  • Save iSkore/5e74c9e2170fe392aa2e07ec9bca0d77 to your computer and use it in GitHub Desktop.
Save iSkore/5e74c9e2170fe392aa2e07ec9bca0d77 to your computer and use it in GitHub Desktop.
Double, isEven, getFileExtension, longestString, arraySum fun functions
'use strict';
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( '.' ).length > 1 ) ? i.substr( i.lastIndexOf( '.' ) + 1 ) : false;
}
function longestString( i ) {
// i will be an array.
// return the longest string in the array
return i.sort( ( a, b ) => {
if( typeof a === 'string' && typeof b === 'string' ) {
return a.length - b.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.
return i.reduce( ( x, y ) => {
return ( !!y.length ) ? ( typeof y === 'object' ) ? x + arraySum( y ) : x : ( typeof y === 'number' ) ? x + y : x;
}, 0 );
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment