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
function chunkArrayInGroups(arr, size) { | |
// A variable to keep the chunked arrays | |
var holder = []; | |
// While the length of the arr is greater than 0 | |
while(arr.length > 0){ | |
// Splice from index 0 to size and push the spliced item into the holder array | |
// arr.splice() mutates the arr | |
holder.push(arr.splice(0, size)); | |
} // End of while loop | |
// Return the holder arr |
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
function truncateString(str, num) { | |
//if num is less or equal to 3 | |
if (num<=3) { | |
//setting up variable word, where str is equal to num letters + ... (starting ar zero, going through num length) | |
var word = str.slice(0, num) + ('...'); | |
//returning word | |
return word; | |
//if num is longer or equal to str length | |
} else if (num >= str.length) { | |
//setting up variable fullWord where str is equal to num letters (starting at 0, going through num length) |
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
function repeatStringNumTimes(str, num) { | |
// Repeat and return if num is greater than 0 and if not, return an empty string | |
return num > 0 ? str.repeat(num) : ""; | |
} | |
repeatStringNumTimes("abc", 3); |
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
function confirmEnding(str, target) { | |
return str.endsWith(target); | |
} | |
/** | |
Alternate solution below | |
function confirmEnding(str, target) { | |
// "Never give up and good luck will find you." | |
// -- Falcor |
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
function largestOfFour(arr){ | |
return arr.map(function(subarr){ | |
return Math.max.apply(null, subarr); | |
}); | |
} | |
largestOfFour([[4, 5, 1, 3], [13, 27, 18, 26], [32, 35, 37, 39], [1000, 1001, 857, 1]]); |
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
function titleCase(str) { | |
return str.split(" ").map(function(each){ | |
return each.replace(each.charAt(0),each.charAt(0).toUpperCase()).replace(each.substr(1,each.length), each.substr(1,each.length).toLowerCase()); | |
}).join(" "); | |
} | |
titleCase("I'm a littLe tea pot"); | |
/** | |
My Old solution below |
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
function findLongestWord(str) { | |
//Split each word into an array | |
var arrayString = str.split(" "); | |
// longest_so_far as declared before looping | |
var longest_so_far = 0; | |
// Let us loop to find the largest items | |
for (var i = 0; i < arrayString.length; i++) { | |
// Is the length of the current item greater than value of longest_so_far? | |
//If so, longest_so_far would now be the value of the length of the current item | |
if(arrayString[i].length > longest_so_far){ longest_so_far = arrayString[i].length;} |
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
function palindrome(str) { | |
// Rid the string of anything not letters or numbers | |
var cleaned = str.toLowerCase().replace(/[^a-z0-9]/g, ""); | |
// Compare the cleaned string to iteself but in a reverse direction, return true or false | |
return (cleaned === cleaned.split("").reverse().join("")); | |
} |
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
/* | |
Here is my first solution without using a forloop | |
function factorialize_for_loop(num) { | |
// Define a variable factor and set an initial value of 1 | |
var factor = 1; | |
// A for loop from 1 to the given number | |
for(var n = 1; n < num+1; n++){ |
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
function reverseString(str) { | |
// Split into an array, reverse, join and return the reversed string | |
return str.split('').reverse().join(''); | |
} | |
reverseString("hello"); |
NewerOlder