aboutsummaryrefslogtreecommitdiff
path: root/writeups/multiprecision/fibo.mjs
diff options
context:
space:
mode:
Diffstat (limited to 'writeups/multiprecision/fibo.mjs')
-rwxr-xr-xwriteups/multiprecision/fibo.mjs33
1 files changed, 12 insertions, 21 deletions
diff --git a/writeups/multiprecision/fibo.mjs b/writeups/multiprecision/fibo.mjs
index 1cc9b10..223581f 100755
--- a/writeups/multiprecision/fibo.mjs
+++ b/writeups/multiprecision/fibo.mjs
@@ -1,37 +1,28 @@
#!/bin/env node
-function do_fibo (num_f) {
+function do_fibo (num_f, str_f) {
let a, b, c;
+ if (typeof str_f === 'undefined') {
+ str_f = (n) => n.toString();
+ }
+
a = num_f('0');
b = num_f('1');
for (let i = 0; i < 101; i += 1) {
c = a + b;
- console.info(c.toString());
+ console.info(str_f(c));
b = a;
a = c;
}
}
-function parse_flag (s) {
- s = s.toLowerCase();
-
- if (s === 'true') {
- return true;
- }
- else if (s === 'false') {
- return false;
- }
+const flag = process.argv.length > 2 ? process.argv[2] : 0;
- return Number(s);
-}
-
-const flag = process.argv.length > 2 ? parse_flag(process.argv[2]) : true;
-
-if (flag) {
- do_fibo(BigInt);
-}
-else {
- do_fibo(Number);
+switch (flag) {
+case '0': do_fibo(Number); break;
+case '1': do_fibo(BigInt); break;
+case '2': do_fibo(Number, (n) => n.toFixed()); break;
+default: throw new Error(process.argv[1] + ": " + flag + ": unknown flag");
}