Skip to content

Instantly share code, notes, and snippets.

View bflannery's full-sized avatar
💭
When in doubt, log it out

Brian bflannery

💭
When in doubt, log it out
View GitHub Profile
@bflannery
bflannery / reverseString.js
Last active December 19, 2016 23:54
Reverse String
// 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 reverseString(str) {
var reverseStr = str.split('').reverse().join('');
return reverseStr;
@bflannery
bflannery / factorizeNumber.js
Created December 20, 2016 00:10
Factorize a Number
// 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) {
@bflannery
bflannery / palindromeChecker.js
Created December 20, 2016 00:41
Palindrome Checker
// Return true if the given string is a palindrome. Otherwise, return false.
function palindrome(str) {
var x = str.replace(/[^a-z0-9+]+/gi, '').toLowerCase();
if(x.split('').reverse().join('') === x) {
return true;
} else {
return false;
}
@bflannery
bflannery / longestWordInAString.js
Created December 20, 2016 02:48
Find the Longest Word in a String
// Return the length of the longest word in the provided sentence.
// Your response should be a number.
function findLongestWord(str) {
var longestWord = str.split(' ').reduce(function(longest, currentWord) {
return currentWord.length > longest.length ? currentWord : longest;
}, "");
return longestWord.length;
}
@bflannery
bflannery / titleCaseASentence.js
Created December 20, 2016 17:16
Return the first letter of each word capitalized
// 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) {
str = str.toLowerCase().split(' ');
for(var i=0; i < str.length; i++) {
str[i]= str[i].charAt(0).toUpperCase() + str[i].slice(1);
@bflannery
bflannery / LargestNumberInArray.js
Created December 20, 2016 18:12
Return Largest Number In An Array
// Return an array consisting of the largest number from each provided sub-array. For simplicity, the provided array will contain 4 sub-arrays.
function largestOfFour(arr) {
let largestNumber = [0,0,0,0];
for(var i=0; i < arr.length; i++) {
for(var j=0; j < arr[i].length; j++) {
if(arr[i][j] > largestNumber[i]) {
largestNumber[i] = arr[i][j];
}
}
@bflannery
bflannery / confimEnding.js
Created December 20, 2016 22:44
Confirm The Ending of a String
// Check if a string (first argument, str) ends with the given target string (second argument, target).
function confirmEnding(str, target) {
return str.substr(-target.length) === target;
}
console.log(confirmEnding("Bastian", "n"));
console.log(confirmEnding("Walking on water and developing software from a specification are easy if both are frozen", "specification"));
@bflannery
bflannery / repeatString.js
Created December 21, 2016 00:03
Repeat a String X Number of Times
// Repeat a given string (first argument) num times (second argument). Return an empty string if num is not a positive number.
function repeatStringNumTimes(str, num) {
let finalStr = "";
while(0 < num) {
finalStr += str;
num--;
}
return finalStr;
}
@bflannery
bflannery / truncateAString.js
Created December 21, 2016 12:38
Truncate A String
function truncateString(str, num) {
// if str.length > str.length num
// return ...
// if the str.length num <= 3
// the dots do Not add to str.length
// console.log(str.length);
// console.log(num);
@bflannery
bflannery / chunkyMonkey.js
Created December 21, 2016 18:10
Split an Array into X amount of SubArrays
// Write a function that splits an array (first argument) into groups the length of size (second argument) and returns them as a two-dimensional array.
function chunkArrayInGroups(arr, size) {
let tempArr = [];
let newArr = [];
for(i=0; i <arr.length; i++) {
if( i % size !== size-1)
tempArr.push(arr[i]);