Created
June 17, 2016 09:21
-
-
Save Oluwasetemi/ee9907bf8addf2cae79f448522f67efa to your computer and use it in GitHub Desktop.
Basic Algorithm Scripting
This file contains 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
function reverseString(str) { | |
var s = str.split("") | |
.reverse() | |
.join(""); | |
return s; | |
} | |
reverseString("hello"); | |
function factorialize(num) { | |
return (num <= 1) ? 1 : num* factorialize(num-1); | |
} | |
factorialize(5); | |
function palindrome(str) { | |
// Good luck! | |
var removeNonNum = str.replace(/[^0-9a-zA-Z]/g, ''); | |
var cleanStr = removeNonNum.toLowerCase(); | |
function reverseStr(clean) { | |
var s = clean.split("").reverse().join(""); | |
return s; | |
} | |
rev = reverseStr(cleanStr); | |
//compare the two strings to give a palindromic verdict | |
return (cleanStr === rev); | |
} | |
palindrome("race car"); | |
function findLongestWord(str) { | |
var array = str.split(" "); | |
var longest = array.reduce(function (a, b) { return a.length > b.length ? a : b; }); | |
//array.sort(function (a, b) { return b.length - a.length; })[0]; | |
return longest.length; | |
} | |
findLongestWord("The quick brown fox jumped over the lazy dog"); | |
function titleCase(str) { | |
var strArr = str.split(" "); | |
var newArr = strArr.map(function(a){ | |
var b = a.toLowerCase(); | |
return b.charAt(0).toUpperCase() + b.slice(1); | |
}); | |
return newArr.join(" "); | |
} | |
titleCase("I'm a little tea pot"); | |
function largestOfFour(arr) { | |
// You can do this! | |
var b = []; | |
for ( var a in arr) { | |
b[a] = arr[a].reduce(function(a,b) {return b > a ? b:a;}); | |
} | |
return b; | |
} | |
largestOfFour([[13, 27, 18, 26], [4, 5, 1, 3], [32, 35, 37, 39], [1000, 1001, 857, 1]]); | |
function confirmEnding(str, target) { | |
// "Never give up and good luck will find you." | |
// -- Falcor | |
return str.substr(-target.length) === target; | |
} | |
confirmEnding("Bastian", "n"); | |
function repeatStringNumTimes(str, num) { | |
// repeat after me | |
return num < 0?"":str.repeat(num); | |
} | |
repeatStringNumTimes("abc", 3); | |
function truncateString(str, num) { | |
// Clear out that junk in your trunk | |
if (str.length > num && num > 3) { | |
return str.slice(0, (num - 3)) + '...'; | |
} else if (str.length > num && num <= 3) { | |
return str.slice(0, num) + '...'; | |
} else { | |
return str; | |
} | |
} | |
truncateString("A-tisket a-tasket A green and yellow basket", 11); | |
function chunkArrayInGroups(arr, size) { | |
// Break it up. | |
var newArray = []; | |
for(var i=0;i<arr.length; i=i+size) { | |
newArray.push(arr.slice(i, i+size)) ; | |
} | |
return newArray; | |
} | |
chunkArrayInGroups(["a", "b", "c", "d"], 2); | |
function slasher(arr, howMany) { | |
// it doesn't always pay to be first | |
return arr.slice(howMany); | |
} | |
slasher([1, 2, 3], 2); | |
function mutation(arr) { | |
var a = arr[0].toLowerCase(); | |
var b = arr[1].toLowerCase(); | |
return b.split("") | |
.every(function(letter) { | |
return a.indexOf(letter) != -1; | |
}); | |
} | |
mutation(["hello", "hey"]); | |
function bouncer(arr) { | |
// Don't show a false ID to this bouncer. | |
var cleanArr = []; | |
for(var i =0; i< arr.length; i++) { | |
if (arr[i]) { | |
cleanArr.push(arr[i]); | |
} | |
} | |
return cleanArr; | |
} | |
Array.prototype.clean = function(deleteValue) { | |
for (var i = 0; i < this.length; i++) { | |
if (this[i] == deleteValue) { | |
this.splice(i, 1); | |
i--; | |
} | |
} | |
return this; | |
}; | |
[7, "ate", "", false, 9].clean(); | |
bouncer([7, "ate", "", false, 9]); | |
function destroyer(arr) { | |
// Remove all the values | |
var args = Array.prototype.slice.call(arguments); | |
args.splice(0,1); | |
return arr.filter(function(element) { | |
return args.indexOf(element) === -1; | |
}); | |
} | |
destroyer([1, 2, 3, 1, 2, 3], 2, 3); | |
function getIndexToIns(arr, num) { | |
// Find my place in this sorted array. | |
arr.sort(function(a,b) { | |
return a-b; | |
}); | |
for (var a = 0; a< arr.length; a++){ | |
if(arr[a] >= num) | |
return parseInt(a); | |
} | |
return arr.length; | |
} | |
//or push num to arr | |
//sort and return the indexof(num) | |
getIndexToIns([2, 5, 10], 15); | |
function rot13(str) { // LBH QVQ VG! | |
var s = str; | |
var x = s.replace(/[a-zA-Z]/g,function(c){return String.fromCharCode((c<="Z"?90:122)>=(c=c.charCodeAt(0)+13)?c:c-26);}); | |
return x; | |
} | |
//other ways | |
// Change the inputs below to test | |
rot13("SERR PBQR PNZC"); | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment