Last active
August 8, 2020 23:37
-
-
Save wkoch/ebab99f10e7bba6a226e5a9c9dbeeb0f to your computer and use it in GitHub Desktop.
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
| // Javascript implementation of the SICP exercises |
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
| console.log(10); | |
| console.log(5 + 3 + 4); | |
| console.log(9 - 1); | |
| console.log(6 / 2); | |
| console.log((2 * 4) + (4 - 6)); | |
| let a = 3; | |
| let b = a + 1; | |
| console.log(a + b + (a * b)) | |
| console.log(a == b) | |
| console.log(((a > b) && (b < (a * b))) ? b : a); | |
| if (a == 4) { | |
| console.log(6); | |
| } else if (b == 4) { | |
| console.log(6 + 7 + a); | |
| } else { | |
| console.log(25); | |
| } | |
| let maior = (a, b) => { return (a >= b ? a : b);} | |
| console.log(2 + (maior(a, b))); |
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
| let square = (n) => {return n * n;} | |
| let sumOfSquares = (a, b, c) => { | |
| if ((a > b) && (b > c)) { | |
| return (square(a)) + (square(b)); | |
| } else if ((a > b) && (b < c)) { | |
| return (square(a)) + (square(c)); | |
| } else { | |
| return (square(b)) + (square(c)); | |
| } | |
| } | |
| console.log(sumOfSquares(2, 3, 4)); |
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
| let average = (x, y) => { | |
| return ((x + y) / 2); | |
| } | |
| let improve = (guess, x) => { | |
| return average(guess, (x / guess)); | |
| } | |
| let square = (x) => { return x * x;} | |
| let good_enough = (guess, x) => { | |
| return (Math.abs(square(guess) - x) < 0.001); | |
| } | |
| let sqrt_iter = (guess, x) => { | |
| if (good_enough(guess, x)) { | |
| return guess; | |
| } else { | |
| return sqrt_iter(improve(guess, x), x); | |
| } | |
| } | |
| let sqrt = (x) => { return sqrt_iter(1.0, x);} | |
| // Examples | |
| console.log(sqrt(9)); | |
| console.log(sqrt(100 + 37)); | |
| console.log(sqrt(sqrt(2)) + sqrt(3)); | |
| console.log(square(sqrt(1000))); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment