Skip to content

Instantly share code, notes, and snippets.

View brainyfarm's full-sized avatar

Olawale Akinseye brainyfarm

View GitHub Profile
@brainyfarm
brainyfarm / chunk_array.js
Created June 21, 2016 14:40
Olawale/FreeCodeCamp Algorithm: Chunk Array in Groups
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
@brainyfarm
brainyfarm / truncate_a_string.js
Last active June 21, 2016 14:37
Olawale/FreeCodeCamp Algorithm: Truncate a String
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)
@brainyfarm
brainyfarm / repeat_a_string
Created June 21, 2016 14:35
Olawale/FreeCodeCamp Algorithm: Repeat a String
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);
@brainyfarm
brainyfarm / confirm_the_ending
Created June 21, 2016 14:16
Olawale/FreeCodeCamp Algorithm: Confirm the Ending
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
@brainyfarm
brainyfarm / largest_number_in_array.js
Created June 21, 2016 14:13
Olawale/FreeCodeCamp Algorithm: Return Largest Number in an Array
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]]);
@brainyfarm
brainyfarm / titlecase_a_sentence.js
Created June 21, 2016 14:10
Olawale/FreeCodeCamp Algorithm: Title Case a Sentence
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
@brainyfarm
brainyfarm / find_longest_word.js
Created June 21, 2016 14:06
Olawale/FreeCodeCamp: Find Longest Word in a String
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;}
@brainyfarm
brainyfarm / check_for_palindrome.js
Created June 21, 2016 14:03
Olawale/FreeCodeCamp: Check for Palindromes
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(""));
}
@brainyfarm
brainyfarm / factorialize_a_number.js
Created June 21, 2016 14:01
Olawale/FreeCodeCamp Algorithm: Factorialize a Number
/*
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++){
@brainyfarm
brainyfarm / reverse_a_string.js
Last active June 21, 2016 13:54
Olawale/FreeCodeCamp Algorithm: Reverse a String
function reverseString(str) {
// Split into an array, reverse, join and return the reversed string
return str.split('').reverse().join('');
}
reverseString("hello");