Skip to content

Instantly share code, notes, and snippets.

View alexitaylor's full-sized avatar
🚀
building cool things

Alexi Taylor alexitaylor

🚀
building cool things
View GitHub Profile
fact(1) = 1
fact(2) = 2 * fact(1)
fact(3) = 3 * fact(2)
fact(4) = 4 * fact(3)
fact(5) = 5 * fact(4)
fact(1) = 1
fact(2) = 2 * 1
fact(3) = 3 * 2 * 1
fact(4) = 4 * 3 * 2 * 1
fact(5) = 5 * 4 * 3 * 2 * 1
fact(n) = n * fact(n - 1)
function fact (n) {
//Base Case
//Recrusive Case
//Code to be executed
}
function fact (n) {
if (n == 1)
return 1;
//Recrusive Case
//Code to be executed
}
function fact (n) {
if (n == 1)
return 1;
else
return n * fact(n - 1);
}
if (numRows === 0) {
return [];
}
var triangle = [[1]];
for(var i = 0; i < numRows - 1; i++ ) {
var row = [1];
for(var j = 1; j < triangle[i].length; j++) {
row[j] = triangle[i][j] + triangle[i][j-1];
function Node(val){
this.value = val;
this.left = null;
this.right = null;
}
function BinaryTree(){
this.root = null;
}
/* Prompt: tackling floating-point imprecision with the CashAmount class
In programming languages such as JavaScript, 0.1 + 0.2 does not equal 0.3. This is true in Ruby, Python, etc. The imprecision is inherent in floating-point arithmetic, it isn't because JavaScript itself is wonky.
These tiny errors can add up and cause actual wrong answers over time if you're not careful. They also make it harder to unit-test your work, since you have to compare within ranges rather than comparing to exact expected values.
To deal with this specifically within a monetary context, let's make a class called CashAmount that accepts double values (e.g., 14.72) and will never suffer from the aforementioned precision problems. A CashAmount represents a collection of bills and coins, such as you might find in your wallet, purse, or a cash register.
Note: you can do this by converting to pennies for all denominations so you are always working with integers, then converting back to a two-decimal float as needed. */
@alexitaylor
alexitaylor / Cardinaldirection.js
Last active April 29, 2017 01:22
Cardinal Direction: get cardinal direction from degrees
//given "0-360" returns the nearest cardinal direction "N/NE/E/SE/S/SW/W/NW/N"
function getCardinal (angle) {
if (angle >= 0 && angle <= 22 || angle >= 338 && angle <= 360)
return "N";
if (angle >= 23 && angle <= 67)
return "NE";
if (angle >= 68 && angle <= 112)
return "E";
if (angle >= 113 && angle <= 157)
return "SE";