-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathmod.ts
45 lines (35 loc) · 842 Bytes
/
mod.ts
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
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
/**
* Get the NTH Fibonacci Number.
*
* @param top The NTH Fibonacci Number.
*/
export function fibonacci(top: number) {
const result = [...fibonacciSequence(top)];
return result[result.length - 1];
}
export function* fibonacciSequence(top: number) {
positiveNumber(top);
let first = 0;
let second = 1;
yield first;
let current = 1;
for (let index = 0; index < top; index++) {
yield second;
current = second;
second = first + second;
first = current;
}
}
/**
* Throws if the number is lower than zero.
*
* @private
*/
function positiveNumber(numberToTest: unknown): asserts numberToTest is number {
if (typeof numberToTest !== "number") {
throw new TypeError("Exepected a number.");
}
if (numberToTest < 0) {
throw new RangeError("Expected a number bigger than zero.");
}
}