Skip to content

Instantly share code, notes, and snippets.

@jgresalfi
jgresalfi / titleCase.js
Created September 6, 2016 19:51
FCC - Title Case A Sentence
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 September 7, 2016 16:07
Function to iterate through nested arrays and find largest values in each.
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 September 7, 2016 16:15
Basic algorithm to count cards in Blackjack.
var count = 0;
function cc(card) {
switch (card) {
case 2:
case 3:
case 4:
case 5:
case 6:
count++;
@jgresalfi
jgresalfi / endConfirm.js
Created September 9, 2016 02:52
Confirm the Ending...match arg2 string with end of arg1 string...or don't...
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 September 14, 2016 01:53
Truncate a strin...
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 September 15, 2016 03:05
Splitting array into two dimensional array based on size parameter
// 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 September 18, 2016 12:06
Verify that all characters in second array element are contained in first element.
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 September 19, 2016 00:40
Remove all falsy values from an array.
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 September 21, 2016 03:13
Remove all elements from the initial array that are of the same value as the arguments that follow array.
function destroyer(args) {
var args = [...arguments]
, finalArray = []
, targetArray = args.shift();
finalArray = targetArray.filter(function(el) {
return args.every(function(arg) {
return ( el !== arg);
});
});
@jgresalfi
jgresalfi / sortAndAdd.js
Last active September 23, 2016 15:45
Return lowest possible index position of second argument after it is added to the first arg's array and sorted...
function getIndexToIns(arr, num) {
let numArr = arguments[0],
addNum = arguments[1];
numArr.push(addNum);
numArr.sort(function(a, b) {
return a - b;
});
return numArr.indexOf(addNum);
}