Last active
February 26, 2018 18:06
-
-
Save rajeebbanstola/77e21a5524de54c7298e08976d99a218 to your computer and use it in GitHub Desktop.
JavaScript Algorithms
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) { | |
splitted = str.split(""); | |
reversed = splitted.reverse(); | |
joined = reversed.join(""); | |
return joined; | |
} | |
reverseString("hello"); | |
function factorialize(num) { | |
if( num == 0 ) return 1; | |
return num * factorialize(num - 1); | |
} | |
factorialize(5); | |
function palindrome(str) { | |
// Good luck! | |
str0 = str.replace(/[\W_]/g, ''); | |
str1 = str0.toLowerCase(); | |
str2 = str1.split(""); | |
str3 = str2.reverse(); | |
str4 = str3.join(""); | |
if( str1 === str4 ){ | |
return true; | |
} else{ | |
return false; | |
} | |
} | |
palindrome("eye"); | |
function findLongestWord(str) { | |
array = str.split(" "); | |
var length = 0; | |
array.map(function(val){ | |
if(val.length > length){ | |
length = val.length; | |
} | |
}); | |
return length; | |
} | |
findLongestWord("The quick brown fox jumped over the lazy dog"); | |
function titleCase(str) { | |
splitted = str.toLowerCase().split(" "); | |
var lowercased = splitted.map(function(value){ | |
return value.replace(value.charAt(0), value.charAt(0).toUpperCase()); | |
}); | |
joined = lowercased.join(" "); | |
return joined; | |
} | |
titleCase("I'm a little tea pot"); | |
function largestOfFour(arr) { | |
// You can do this! | |
var final = [ ]; | |
for(i = 0; i < arr.length; i++){ | |
var largeNum = 0; | |
for(j=0; j < arr[i].length; j++){ | |
if(arr[i][j] > largeNum){ | |
largeNum = arr[i][j]; | |
} | |
} | |
final.push(largeNum); | |
} | |
return final; | |
} | |
largestOfFour([[4, 5, 1, 3], [13, 27, 18, 26], [32, 35, 37, 39], [1000, 1001, 857, 1]]); | |
function confirmEnding(str, target) { | |
// "Never give up and good luck will find you." | |
// -- Falcor | |
stringlength = str.length; | |
string = str.substr(stringlength-target.length, target.length); | |
if(string == target){ | |
return true; | |
} else{ | |
return false; | |
} | |
} | |
confirmEnding("Bastian", "n"); | |
function repeatStringNumTimes(str, num) { | |
// repeat after me | |
if(num > 0 ){ | |
str1 = ""; | |
for(i=0; i < num; i++){ | |
str1 += str; | |
} | |
return str1; | |
} else { | |
return ""; | |
} | |
} | |
repeatStringNumTimes("abc", 3); | |
function truncateString(str, num) { | |
// Clear out that junk in your trunk | |
if(str.length > num && num > 3){ | |
reduced = str.slice(0, num-3) + "..."; | |
} else if(str.length > num && num <= 3) { | |
reduced = str.slice(0, num) + "..."; | |
} else{ | |
reduced = str; | |
} | |
return reduced; | |
} | |
truncateString("A-tisket a-tasket A green and yellow basket", 11); | |
function chunkArrayInGroups(arr, size) { | |
// Break it up. | |
var newArray = []; | |
for(i = 0; i < arr.length; i+=size){ | |
var items = arr.slice(i, i+size); | |
newArray.push(items); | |
} | |
return newArray; | |
} | |
chunkArrayInGroups(["a", "b", "c", "d"], 2); | |
function slasher(arr, howMany) { | |
// it doesn't always pay to be first | |
return arr.splice(howMany, arr.length-howMany); | |
} | |
slasher([1, 2, 3], 2); | |
function mutation(arr) { | |
var lowercased1 = arr[1].toLowerCase(); | |
var lowercased0 = arr[0].toLowerCase(); | |
for(i=0; i < lowercased1.length; i++){ | |
if (lowercased0.indexOf(lowercased1[i]) === -1){ | |
return false; | |
} | |
} | |
return true; | |
} | |
mutation(["hello", "hey"]); | |
function bouncer(arr) { | |
return arr.filter(function(value){ | |
if (value){ | |
return (value); | |
} | |
}); | |
} | |
bouncer([7, "ate", "", false, 9]); | |
function destroyer(arr) { | |
var args = Array.prototype.slice.call(arguments); | |
for (var i = 0; i < arr.length; i++) { | |
for (var j = 0; j < args.length; j++) { | |
if (arr[i] === args[j]) { | |
delete arr[i]; | |
} | |
} | |
} | |
return arr.filter(Boolean); | |
} | |
destroyer([1, 2, 3, 1, 2, 3], 2, 3); | |
function getIndexToIns(arr, num) { | |
// Find my place in this sorted array. | |
sortedArray = arr.sort(function(a, b){return a-b}); | |
sortedArray.push(num); | |
sortedArray = sortedArray.sort(function(a, b){return a-b}); | |
position = sortedArray.indexOf(num); | |
return position; | |
} | |
getIndexToIns([40, 60], 50); | |
function rot13(str) { // LBH QVQ VG! | |
array = str.split(""); | |
return array.map(function(value){ | |
x = value.charCodeAt(0); | |
// Checks if character lies between A-Z | |
if (x < 65 || x > 90) { | |
return String.fromCharCode(x); // Return un-converted character | |
} | |
//N = ASCII 78, if the character code is less than 78, shift forward 13 places | |
else if (x < 78) { | |
return String.fromCharCode(x + 13); | |
} | |
return String.fromCharCode(x - 13); | |
}).join(""); | |
} | |
// 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