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 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 |
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 fib(n, sum = 0, prev = 1) { | |
| if (n <= 1) return sum; | |
| return fib(n - 1, prev + sum, sum); | |
| } | |
| console.log(fib(20000)); |
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 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); | |
| } | |
| } |
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 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; | |
| } |
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
| /* | |
| * 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 |