-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathfibonacci.js
29 lines (28 loc) · 858 Bytes
/
fibonacci.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
const fibonacci = function* (times = undefined, showwarn = false) {
const count = ({ i, a0, a1 }) => ({ i: i + 1, a0: a1, a1: a0 + a1 });
let fib0 = { i: 0, a0: -1, a1: 1 };
let fib;
let test;
try {
if (times === undefined) {
showwarn && console.warn('endless fibonacci sequence was created')
test = () => true;
} else if (typeof times !== 'number') {
throw new Error('times need a number');
} else if (times >= 0) {
test = ({ i }) => i <= times;
} else {
console.error('A Negative Number Was Received.');
showwarn && console.warn('show test fibonacci sequence');
test = ({ i }) => i <= 5;
}
} catch (e) {
console.error('An Incorrect Serial Number Was Received.');
} finally {
while (test(fib0)) {
fib = count(fib0);
yield fib.a1;
fib0 = fib;
}
}
}