Skip to content

Instantly share code, notes, and snippets.

@KimSarabia
KimSarabia / fib.js
Last active October 11, 2017 21:13
Constant Fibonacci
// https://www.mathsisfun.com/numbers/golden-ratio.html
// Use Golden Ratio to Find Fibonacci given n is term number
function fibonacci(n) {
return Math.round(
(Math.pow((1 + Math.sqrt(5)) / 2, n) —
Math.pow(-2 / (1 + Math.sqrt(5)), n)) /
Math.sqrt(5)
);
}
@KimSarabia
KimSarabia / combinations.js
Created October 11, 2017 14:58 — forked from axelpale/combinations.js
JavaScript functions to calculate combinations of elements in Array.
/**
* Copyright 2012 Akseli Palén.
* Created 2012-07-15.
* Licensed under the MIT license.
*
* <license>
* Permission is hereby granted, free of charge, to any person obtaining
* a copy of this software and associated documentation files
* (the "Software"), to deal in the Software without restriction,
* including without limitation the rights to use, copy, modify, merge,
@KimSarabia
KimSarabia / best-travel.js
Created October 10, 2017 20:30
Best Travel
// Work in Progress
function chooseBestSum(t, k, ls) {
if(ls.length <= 1){return null}
else {
return Array
.apply(0, { length: Math.pow(k+1, ls.length) })
.map(Number.call, Number)
.map(a => a
.toString(ls.length)
@KimSarabia
KimSarabia / pyramid-slide-down.js
Last active October 10, 2017 02:47
pyramid-slide-down.js
function longestSlideDown (pyramid) {
var pyramidClone = pyramid.slice();
for(var i = pyramid.length - 2; i >= 0; i--){
current = pyramid[i]
if(i === pyramid.length - 2){
next = pyramidClone[i+1]
} else {
next = newArr
}
newRow = []
@KimSarabia
KimSarabia / no-special-chars
Created October 4, 2017 20:16
Reject Special Characters Regex
Determine if any characters are not in character classes a-z, A-Z or 0-9
This will also treat é or similar characters as rejected symbols.
if(!/[^a-zA-Z0-9]/.test($(this).val())) {
$("#passwordErrorMsg").html("OK");
}
Source: https://stackoverflow.com/questions/19932596/regex-to-not-allow-special-characters-javascript
@KimSarabia
KimSarabia / parseInt
Last active June 14, 2023 14:07
ParseInt Tricky Use Case & Converting String of Numbers to Numbers
//From Map Article: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/map
// Consider:
['1', '2', '3'].map(parseInt);
// While one could expect [1, 2, 3]
// The actual result is [1, NaN, NaN]
// parseInt is often used with one argument, but takes two.
// The first is an expression and the second is the radix.
// To the callback function, Array.prototype.map passes 3 arguments:
// the element, the index, the array
@KimSarabia
KimSarabia / for-in
Created October 2, 2017 20:11
Iterate over the properties of an object
//Use For-In
var buz = {
fog: 'stack'
};
for (var name in buz) {
if (buz.hasOwnProperty(name)) {
console.log('this is fog (' +
name + ') for sure. Value: ' + buz[name]);
}
@KimSarabia
KimSarabia / fave cake answer
Created October 2, 2017 20:10
For-In Example - Comparing Two Objects
//Favorite Pete the Baker Solution
//https://www.codewars.com/kata/525c65e51bf619685c000059/solutions/javascript
function cakes(recipe, available) {
var numCakes = [];
for(var key in recipe){
if(recipe.hasOwnProperty(key)){
if(key in available){
numCakes.push(Math.floor(available[key] / recipe[key]));
@KimSarabia
KimSarabia / minimum
Created October 2, 2017 18:56
Finding the minimum in an array
//Source: https://stackoverflow.com/questions/8934877/obtain-smallest-value-from-array-in-javascript
Array.min = function( array ){
return Math.min.apply( Math, array );
};
var minimum = Array.min(array);
@KimSarabia
KimSarabia / tally
Last active September 29, 2017 19:43
Creating Tally
//From Reduce - https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/Reduce?v=a
//Counting instances of values in an object
//Initializing with an empty object
var names = ['Alice', 'Bob', 'Tiff', 'Bruce', 'Alice'];
var countedNames = names.reduce(function (allNames, name) {
if (name in allNames) {
allNames[name]++;
}