Skip to content

Instantly share code, notes, and snippets.

@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 / 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 / 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 / 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 / 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 / longWord.js
Last active September 5, 2016 02:45
Find longest word in a string.
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 / phoneBuyerz.js
Created September 4, 2016 20:23
Silly phone buying app...
"use strict";
const SALESTAX = .0975,
PHONE_PRICE = 200.00,
ACCESS_PRICE = 40.00;
var ba = prompt("How much money you got?"),
ac = prompt("When to stop buying accessories?"),
bankAccount = parseInt(ba),
accessoryLimit = parseInt(ac),
@jgresalfi
jgresalfi / reverseStr.js
Created September 4, 2016 20:22
Reversing strings
function reverseString(str) {
var strArray = str.split("");
strArray.reverse("");
return strArray.join("");
}
reverseString("hello");
@jgresalfi
jgresalfi / recordCol.js
Created September 4, 2016 20:22
Modifying objects/checking properties - record collection example
var collection = {
"2548": {
"album": "Slippery When Wet",
"artist": "Bon Jovi",
"tracks": [
"Let It Rock",
"You Give Love a Bad Name"
]
},
"2468": {
@jgresalfi
jgresalfi / factorializer.js
Created September 4, 2016 20:20
Factorial! factory.
function factorialize(num) {
var facArray = [];
if (num === 0) {
return 1;
} else {
for (var i = 1; i <= num; i++) {
facArray.push(i);