This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// 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) | |
); | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/** | |
* 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, |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// 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) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 = [] |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
//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 |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
//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]); | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
//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])); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
//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); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
//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]++; | |
} |