Skip to content

Instantly share code, notes, and snippets.

Avatar

Jason Gresalfi jgresalfi

View GitHub Profile
@jgresalfi
jgresalfi / longWord.js
Last active Sep 5, 2016
Find longest word in a string.
View longWord.js
function findLongestWord(str) {
var strArray = str.split(" "),
arrCount = [];
for (var i = 0; i < strArray.length; i++) {
arrCount.push(strArray[i].length);
}
return Math.max(...arrCount);
}
findLongestWord("The quick brown fox jumped over the lazy dog");
@jgresalfi
jgresalfi / titleCase.js
Created Sep 6, 2016
FCC - Title Case A Sentence
View titleCase.js
var endArr = [],
firstCap = [],
finalStr = [];
function theChopper(arr) {
for (var i = 0; i < arr.length; i++) {
firstCap.push(arr[i].charAt(0).toUpperCase());
endArr.push(arr[i].slice(1));
}
}
@jgresalfi
jgresalfi / largestOfFour.js
Created Sep 7, 2016
Function to iterate through nested arrays and find largest values in each.
View largestOfFour.js
function largestOfFour(arr) {
for (var i = 0; i < arr.length; i++) {
for (var j = 0; j < arr[i].length; j++) {
arr[i] = Math.max(...arr[i]);
}
}
return arr;
}
largestOfFour([[4, 5, 1, 3], [13, 27, 18, 26], [32, 35, 37, 39], [1000, 1001, 857, 1]]);
@jgresalfi
jgresalfi / cardCounting.js
Created Sep 7, 2016
Basic algorithm to count cards in Blackjack.
View cardCounting.js
var count = 0;
function cc(card) {
switch (card) {
case 2:
case 3:
case 4:
case 5:
case 6:
count++;
@jgresalfi
jgresalfi / endConfirm.js
Created Sep 9, 2016
Confirm the Ending...match arg2 string with end of arg1 string...or don't...
View endConfirm.js
function confirmEnding(str, target) {
var fragCount = target.length,
chunk = str.substring(str.length - fragCount);
if (chunk === target) {
return true;
} else { return false; }
}
confirmEnding("Walking on water and developing software from a specification are easy if both are frozen", "specification");
@jgresalfi
jgresalfi / truncateStr.js
Created Sep 14, 2016
Truncate a strin...
View truncateStr.js
function truncateString(str, num) {
if (num <= 3) {
str = str.slice(0, num) + "...";
} else if (str.length > num) {
str = str.slice(0, (num - 3)) + "...";
}
return str;
}
truncateString("Jason", 1);
@jgresalfi
jgresalfi / chunkyMonkey.js
Created Sep 15, 2016
Splitting array into two dimensional array based on size parameter
View chunkyMonkey.js
// 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) {
var newArr = [];
beginSlice = 0,
endSlice = size;
for (var i = 0; i < (arr.length/size); i++) {
newArr.push(arr.join("").slice(beginSlice, endSlice).split());
beginSlice += size;
@jgresalfi
jgresalfi / mutations.js
Created Sep 18, 2016
Verify that all characters in second array element are contained in first element.
View mutations.js
function mutation(arr) {
var str1 = arr.slice(0, 1).join(" ").toLowerCase(),
str2 = arr.slice(1).join(" ").toLowerCase(),
hits = 0;
for (var i = 0; i < str2.length; i++) {
if (str1.indexOf(str2[i]) !== -1) {
hits += 1;
}
}
if (hits === str2.length) {
@jgresalfi
jgresalfi / falsyBouncer.js
Created Sep 19, 2016
Remove all falsy values from an array.
View falsyBouncer.js
function bouncer(arr) {
var newArr = arr.filter( function(el) {
if (el) {
return el;
}
});
return newArr;
}
bouncer([7, "ate", "", false, 9]);
@jgresalfi
jgresalfi / seekAndDestroy.js
Created Sep 21, 2016
Remove all elements from the initial array that are of the same value as the arguments that follow array.
View seekAndDestroy.js
function destroyer(args) {
var args = [...arguments]
, finalArray = []
, targetArray = args.shift();
finalArray = targetArray.filter(function(el) {
return args.every(function(arg) {
return ( el !== arg);
});
});