diff --git a/src/fib.ts b/src/fib.ts index 8dc8ddd..57a0d91 100644 --- a/src/fib.ts +++ b/src/fib.ts @@ -1,12 +1,27 @@ // util function that computes the fibonacci numbers -module.exports = function fibonacci(n) { +function fibonacci(n: number): number { + if (typeof n !== 'number' || isNaN(n)) { + throw new TypeError('Input must be a valid number'); + } + if (n < 0) { - return -1; - } else if (n == 0) { + throw new RangeError('Input must be a non-negative integer'); + } else if (n === 0) { return 0; - } else if (n == 1) { + } else if (n === 1) { return 1; } - return fibonacci(n - 1) + fibonacci(n - 2); -}; + // Removed redundant type annotations + let a = 0; + let b = 1; + let temp: number; + + for (let i = 2; i <= n; i++) { + temp = a + b; + a = b; + b = temp; + } + return b; +} +export default fibonacci;