Last active
December 30, 2015 06:13
-
-
Save codewithtyler/01f8a535e0dad79282ac to your computer and use it in GitHub Desktop.
Beginner Algorithm Challenges - All Done in JavaScript
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
/* | |
* Instructions: | |
* Remove all falsy values from an array. | |
* Falsy values in javascript are false, null, 0, "", undefined, and NaN. | |
*/ | |
function bouncer(arr) { | |
arr = arr.filter(function(val) { | |
return Boolean(val); | |
}); | |
return arr; | |
} |
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
/* | |
* Instructions: | |
* Return the provided string with the first letter of each word capitalized. Make sure | |
* the rest of the word is in lower case. For the purpose of this exercise, you should | |
* also capitalize connecting words like "the" and "of". | |
*/ | |
function titleCase(str) { | |
var arrayOfStrings = str.toLowerCase().split(" "); | |
for (var i = 0; i < arrayOfStrings.length; i++) | |
{ | |
arrayOfStrings[i] = arrayOfStrings[i].charAt(0).toUpperCase() + arrayOfStrings[i].substr(1); | |
} | |
return arrayOfStrings.join(" "); | |
} |
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
/* | |
* Instructions: | |
* Write a function that splits an array (first argument) into groups the length of | |
* size (second argument) and returns them as a multidimensional array. | |
*/ | |
function chunk(arr, size) { | |
var array = []; | |
while (arr.length) | |
{ | |
array.push( arr.splice(0, size) ); | |
} | |
return array; | |
} |
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
/* | |
* Instructions: | |
* Check if a string (first argument) ends with the given target string (second argument). | |
*/ | |
function end(str, target) { | |
return str.substr(0 - target.length) === target; | |
} |
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
/* | |
* Instructions: | |
* You will be provided with an initial array (the first argument in the destroyer function), | |
* followed by one or more arguments. Remove all elements from the initial array that are of | |
* the same value as these arguments. | |
*/ | |
function destroyer(arr) { | |
// Get the arguments | |
var args = Array.prototype.slice.call(arguments); | |
args.splice(0, 1); | |
// Create a clean array to store the values | |
var temp = []; | |
// Compare element against the original array | |
for (var i = 0; i < arr.length; i++) | |
{ | |
for (var j = 0; j < args.length; j++) | |
{ | |
if (arr[i] === args[j]) | |
{ | |
delete arr[i]; | |
} | |
} | |
} | |
temp = arr.filter(bouncer); | |
return temp; | |
} | |
function bouncer(val) { | |
return Boolean(val); | |
} |
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
/* | |
* Instructions: | |
* Return the factorial of the provided integer. | |
* If the integer is represented with the letter n, a factorial is the product of | |
* all positive integers less than or equal to n. Factorials are often represented | |
* with the shorthand notation n! | |
* For example: 5! = 1 * 2 * 3 * 4 * 5 = 120 | |
*/ | |
function factorialize(num) { | |
var answer = 1; | |
for (var i = 1; i <= num; i++) | |
{ | |
answer *= i; | |
} | |
return answer; | |
} |
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
/* | |
* Instructions: | |
* Return an array consisting of the largest number from each provided sub-array. | |
* For simplicity, the provided array will contain exactly 4 sub-arrays. | |
* Remember, you can iterate through an array with a simple for loop, and access | |
* each member with array syntax arr[i] .* | |
*/ | |
function largestOfFour(arr) { | |
for (var i = 0; i < arr.length; i++) | |
{ | |
var largest = -1; | |
for (var j = 0; j < arr[i].length; j++) | |
{ | |
if (arr[i][j] > largest) | |
{ | |
largest = arr[i][j]; | |
} | |
} | |
arr[i] = largest; | |
} | |
return arr; | |
} | |
largestOfFour([[4, 5, 1, 3], [13, 27, 18, 26], [32, 35, 37, 39], [1000, 1001, 857, 1]]); |
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
/* | |
* Instructions: | |
* Return the length of the longest word in the provided sentence. | |
* Your response should be a number. | |
*/ | |
function findLongestWord(str) { | |
var words = str.split(" "); | |
var word = ""; | |
var longestWord; | |
for (var i = 0; i < words.length; i++) | |
{ | |
if (words[i].length > word.length) | |
{ | |
word = words[i]; | |
longestWord = word.length; | |
} | |
} | |
return longestWord; | |
} |
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
/* | |
* Instructions: | |
* 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. For example, ["hello", "Hello"], should | |
* return true because all of the letters in the second string are present in the first, | |
* ignoring case. The arguments ["hello", "hey"] should return false because the string "hello" | |
* does not contain a "y". Lastly, ["Alien", "line"], should return true because all of the | |
* letters in "line" are present in "Alien". | |
*/ | |
function mutation(arr) { | |
// Ignore capitalization | |
var word = arr[0].toLowerCase(); | |
var testWord = arr[1].toLowerCase(); | |
word = word.split(""); | |
testWord = testWord.split(""); | |
// Compare the words and see if the letters in test word are also in the first word | |
for (var i = 0; i < testWord.length; i++) | |
{ | |
// Test to see if the letter is in the first word | |
if (word.indexOf(testWord[i]) == -1) | |
return false; // It's not there. | |
} | |
return true; // It is there. | |
} |
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
/* | |
* Instructions: | |
* Return true if the given string is a palindrome. Otherwise, return false. | |
* A palindrome is a word or sentence that's spelled the same way both forward | |
* and backward, ignoring punctuation, case, and spacing. You'll need to remove | |
* all non-alphanumeric characters (punctuation, spaces and symbols) and turn | |
* everything lower case in order to check for palindromes. | |
*/ | |
function palindrome(str) { | |
str = str.replace(/[^a-zA-Z0-9]+/gi, '').toLowerCase(); | |
return str == str.split('').reverse().join(''); | |
} |
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
/* | |
* Instructions: | |
* Repeat a given string (first argument) n times (second argument). | |
* Return an empty string if n is a negative number. | |
*/ | |
function repeat(str, num) { | |
var text = ""; | |
for (var i = 0; i < num; i++) | |
{ | |
text += str; | |
} | |
return text; | |
} |
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
/* | |
* Instructions: | |
* Reverse the provided string. | |
* You may need to turn the string into an array before you can reverse it. | |
* Your result must be a string. | |
*/ | |
function reverse(str) { | |
return str.split("").reverse().join(""); | |
} |
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
/* | |
* Instructions: | |
* Return the remaining elements of an array after chopping off n elements from the head. | |
* The head meaning the beginning of the array, or the zeroth index. | |
*/ | |
function slasher(arr, howMany) { | |
return arr.splice(howMany); | |
} |
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
/* | |
* Instructions: | |
* Truncate a string (first argument) if it is longer than the given maximum string | |
* length (second argument). Return the truncated string with a "..." ending. | |
* Note that the three dots at the end add to the string length. | |
* If the num is less than or equal to 3, then the length of the three dots is not | |
* added to the string length. | |
*/ | |
function truncate(str, num) { | |
if (num <= 3) | |
{ | |
return str.substr(0, num) + "..."; | |
} | |
else | |
{ | |
return str.length > num ? str.substr(0, num - 3) + "..." : str.substr(0, num); | |
} | |
} |
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
/* | |
* Instructions: | |
* Return the lowest index at which a value (second argument) should be inserted into an | |
* array (first argument) once it has been sorted. For example, where([1,2,3,4], 1.5) | |
* should return 1 because it is greater than 1 (index 0), but less than 2 (index 1). | |
* Likewise, where([20,3,5], 19) should return 2 because once the array has been sorted | |
* it will look like [3,5,20] and 19 is less than 20 (index 2) and greater than 5 (index 1). | |
*/ | |
function where(arr, num) { | |
arr.push(num); | |
arr.sort(function(a, b) { | |
return a - b; | |
}); | |
return arr.indexOf(num); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment