Last active
March 2, 2016 19:20
-
-
Save bugyt/00a3196545e6a6ddb0f9 to your computer and use it in GitHub Desktop.
JS: Basic Algorithm Scripting
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
//////////////////////////////////////////////////////////////// | |
// Reverse a String | |
//////////////////////////////////////////////////////////////// | |
function reverseString(str) { | |
var myArray = str.split(''); | |
myArray.reverse(); | |
str = myArray.join(''); | |
return str; | |
} | |
// Use | |
reverseString("hello"); | |
//////////////////////////////////////////////////////////////// | |
// Factorialize a Number | |
//////////////////////////////////////////////////////////////// | |
function factorialize(num) { | |
var result=1; | |
for (var ii=2; ii<=num;ii++){ | |
result*=ii; | |
} | |
return result; | |
} | |
// Use | |
factorialize(5); | |
//////////////////////////////////////////////////////////////// | |
// Check for Palindromes | |
//////////////////////////////////////////////////////////////// | |
function palindrome(str) { | |
var strReverse; | |
var myArray; | |
str=str.toLowerCase(); | |
str=str.replace(/(\W)/gi,""); | |
str=str.replace("_",""); | |
console.log(str); | |
myArray = str.split(''); | |
myArray.reverse(); | |
strReverse = myArray.join(''); | |
if (str === strReverse){ | |
return true; | |
} | |
return false; | |
} | |
// Use | |
palindrome("eye"); | |
//////////////////////////////////////////////////////////////// | |
// Find the Longest Word in a String | |
//////////////////////////////////////////////////////////////// | |
function findLongestWord(str) { | |
var myArray = str.split(' '); | |
var result = 0; | |
for(var ii= 0; ii<myArray.length; ii++){ | |
if (myArray[ii].length>result) { | |
result = myArray[ii].length; | |
} | |
} | |
return result; | |
} | |
// Use | |
findLongestWord("The quick brown fox jumped over the lazy dog"); | |
//////////////////////////////////////////////////////////////// | |
// Title Case a Sentence | |
//////////////////////////////////////////////////////////////// | |
function titleCase(str) { | |
var myArray = str.split(' '); | |
myArray = myArray.map(function(val){ | |
val = val.toLowerCase(); | |
return val.charAt(0).toUpperCase() + val.slice(1); | |
}); | |
str = myArray.join(' '); | |
return str; | |
} | |
// Use | |
titleCase("I'm a little tea pot"); | |
//////////////////////////////////////////////////////////////// | |
// Return Largest Numbers in Arrays | |
//////////////////////////////////////////////////////////////// | |
function largestNumbersInArrays(arr) { | |
var newArray=[]; | |
function sortDescNumber(a,b) { | |
return b - a; | |
} | |
for (var ii=0; ii < arr.length; ii++) { | |
arr[ii].sort(sortDescNumber); | |
newArray.push(arr[ii][0]); | |
} | |
// You can do this! | |
return newArray; | |
} | |
// Use | |
largestNumbersInArrays([[4, 5, 1, 3], [13, 27, 18, 26], [32, 35, 37, 39], [1000, 1001, 857, 1]]); | |
//////////////////////////////////////////////////////////////// | |
// Check if a string (first argument) ends with the given | |
// target string (second argument). | |
//////////////////////////////////////////////////////////////// | |
function end(str, target) { | |
if (str.substr(str.length - target.length, target.length ) == target){ | |
return true; | |
} | |
return false; | |
} | |
// Use | |
end("Bastian", "n"); | |
//////////////////////////////////////////////////////////////// | |
// Repeat a string | |
//////////////////////////////////////////////////////////////// | |
function repeat(str, num) { | |
var strRes = ""; | |
if (num<1) { | |
return ""; | |
} | |
for (var ii=0; ii < num; ii++){ | |
strRes = strRes + str; | |
} | |
return strRes; | |
} | |
// Use | |
repeat("abc", 3); | |
//////////////////////////////////////////////////////////////// | |
// Truncate a string | |
//////////////////////////////////////////////////////////////// | |
function truncate(str, num) { | |
var cplmt = "..."; | |
if (str.length<=num){ | |
return str; | |
} | |
str = str.slice(0, num - (cplmt.length * (num>3))) + cplmt; | |
return str; | |
} | |
// Use | |
truncate("A-tisket a-tasket A green and yellow basket", 11); | |
//////////////////////////////////////////////////////////////// | |
// Splits an array (first argument) into groups the length of | |
// size (second argument) and returns them as a two-dimensional array. | |
//////////////////////////////////////////////////////////////// | |
function chunk(arr, size) { | |
var arrayRes = []; | |
for (var ii=0; ii < arr.length; ii += size){ | |
arrayRes.push(arr.slice(ii,ii+size)); | |
} | |
return arrayRes; | |
} | |
// Use | |
chunk(["a", "b", "c", "d"], 2); | |
//////////////////////////////////////////////////////////////// | |
// Return the remaining elements of an array after chopping off | |
// n elements from the head. | |
//////////////////////////////////////////////////////////////// | |
function slasher(arr, howMany) { | |
arr.splice(0,howMany); | |
return arr; | |
} | |
// Use | |
slasher([1, 2, 3], 2); | |
//////////////////////////////////////////////////////////////// | |
// Mutations : | |
// Return true if the string in the first element of the array | |
// contains all of the letters of the string in the second | |
// element of the array. | |
//////////////////////////////////////////////////////////////// | |
function mutation(arr) { | |
var tmpArray = arr[1].toLowerCase().split(''); | |
var res = true; | |
tmpArray.forEach(function (element, index, array){ | |
if (arr[0].toLowerCase().indexOf(element)<0){ | |
res = false; | |
} | |
}); | |
return res; | |
} | |
// Use | |
mutation(["hello", "hey"]); | |
//////////////////////////////////////////////////////////////// | |
// Remove all falsy values from an array. | |
//////////////////////////////////////////////////////////////// | |
function bouncer(arr) { | |
var filtered = arr.filter(function (element, index, array){ | |
return element; | |
}); | |
return filtered; | |
} | |
// Use | |
bouncer([7, "ate", "", false, 9]); | |
//////////////////////////////////////////////////////////////// | |
// Seek and Destroy : | |
// Remove all elements from the initial array that are of the | |
// same value as these arguments. | |
//////////////////////////////////////////////////////////////// | |
function destroyer(arr) { | |
var args = arguments; | |
var myArray = arguments[0]; | |
var seek = function(element){ | |
return (element!=args[this]); | |
}; | |
for (var ii=1; ii< arguments.length; ii++){ | |
myArray = myArray.filter(seek, ii); | |
} | |
return myArray; | |
} | |
// Use | |
destroyer([1, 2, 3, 1, 2, 3], 2, 3); | |
//////////////////////////////////////////////////////////////// | |
// Where do I belong : | |
// Return the lowest index at which a value (second argument) | |
// should be inserted into an array (first argument) once | |
// it has been sorted. | |
//////////////////////////////////////////////////////////////// | |
function where(arr, num) { | |
// Find my place in this sorted array. | |
function sortAscNumber(a,b) { | |
return a - b; | |
} | |
arr.sort(sortAscNumber); | |
for (var ii=0; ii < arr.length; ii++) { | |
if (num<=arr[ii]) { | |
return ii; | |
} else if ((ii +1) > (arr.length -1)) { | |
return ii+1; | |
} else if (num>arr[ii] && num<=arr[ii+1]) { | |
return ii+1; | |
} | |
} | |
} | |
// Use | |
where([40, 60], 50); | |
//////////////////////////////////////////////////////////////// | |
// Caesars Cipher : takes a ROT13 encoded string as input and | |
// returns a decoded string. | |
//////////////////////////////////////////////////////////////// | |
function rot13(str) { // LBH QVQ VG! | |
str = str.replace(/[a-zA-Z]/g,function(c){return String.fromCharCode((c<="Z"?90:122)>=(c=c.charCodeAt(0)+13)?c:c-26);}); | |
return str; | |
} | |
// Use | |
rot13("Gb trg gb gur bgure fvqr!"); | |
//////////////////////////////////////////////////////////////// | |
// | |
//////////////////////////////////////////////////////////////// |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment