Skip to content

Instantly share code, notes, and snippets.

View juanelojga's full-sized avatar

Juan Almeida juanelojga

View GitHub Profile
@juanelojga
juanelojga / non-tco-fibonacci.js
Created August 1, 2018 02:45
Fibonacci Sequence without TCO
function fib(n) {
if (n <= 0) return 0;
if (n === 1 || n === 2) return 1;
return fib(n - 1) + fib(n - 2);
}
console.log(fib(20000)); //ERROR: maximum call stack size exceeded
@juanelojga
juanelojga / fibonacci-tco.js
Created August 1, 2018 02:38
TCO Fibonacci Sequence
function fib(n, sum = 0, prev = 1) {
if (n <= 1) return sum;
return fib(n - 1, prev + sum, sum);
}
console.log(fib(20000));
@juanelojga
juanelojga / recursion-factorial.js
Created July 31, 2018 02:40
Recursive solution of factorial
function computeFactorial(num) {
if (num === 1) {
console.log("hitting de base case");
return 1;
} else {
console.log(`returning ${num} * computeFactorial(${num - 1})`);
return num * computeFactorial(num - 1);
}
}
@juanelojga
juanelojga / iterative-factorial.js
Created July 31, 2018 02:34
Iterative solution of factorial
function computeFactorial(num) {
// iterative solution of factorial
let result = 1;
for (let i = 2; i <= num; i++) {
console.log(`result = ${result} = ${i} * (${result * i})`);
result *= i;
}
return result;
}
@juanelojga
juanelojga / base64-image-upload.js
Created June 30, 2017 20:45 — forked from madhums/base64-image-upload.js
save base64 encoded image
/*
* Taken from http://stackoverflow.com/questions/5867534/how-to-save-canvas-data-to-file/5971674#5971674
*/
var fs = require('fs');
// string generated by canvas.toDataURL()
var img = "data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAABQAAAAUCAYAAACNiR0"
+ "NAAAAKElEQVQ4jWNgYGD4Twzu6FhFFGYYNXDUwGFpIAk2E4dHDRw1cDgaCAASFOffhEIO"
+ "3gAAAABJRU5ErkJggg==";
// strip off the data: url prefix to get just the base64-encoded bytes