Created
July 26, 2021 18:56
-
-
Save wperron/af0bfe952cf5284ffe64ec39a26ba7f8 to your computer and use it in GitHub Desktop.
Fibonnaci with Deno
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
import { cyan } from "https://deno.land/[email protected]/fmt/colors.ts"; | |
export function fib(n: number): number { | |
if (0 <= n && n <= 1) { | |
return n; | |
} | |
let a = 0, b = 1; | |
for (let i = 0; i < n; i++) { | |
[a, b] = [b, a + b]; | |
} | |
return a; | |
} | |
if (import.meta.main) { | |
console.log(cyan(`${fib(parseInt(Deno.args[0]))}`)); | |
} |
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
import { | |
assertEquals | |
} from "https://deno.land/[email protected]/testing/asserts.ts"; | |
import { fib } from "./fibonacci.ts"; | |
Deno.test({ | |
name: "fibonacci numbers", | |
fn() { | |
assertEquals(fib(0), 0); | |
assertEquals(fib(1), 1); | |
assertEquals(fib(6), 8); | |
assertEquals(fib(10), 55); | |
} | |
}); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment