diff --git a/fibonacci.ts b/fibonacci.ts new file mode 100644 index 00000000..adb22c5a --- /dev/null +++ b/fibonacci.ts @@ -0,0 +1,39 @@ +/** + * A function to get Fibonacci value of given number + * @param number The input integer + * @return {number} Fibonacci value of `number` + * @example fibonacciValue(4) => 3 | fibonacciValue(6) => 8 + * @see https://en.m.wikipedia.org/wiki/Fibonacci_number + * @author MohdFaisalBidda + */ + + +try { + const fibonacciValue = (number: number): number => { + if (number == 0) { + return 0; + } + + if (number < 0) { + return "Number should be greater than 0"; + } + + let a = 0; + + let b = 1; + + for (let i = 1; i < number; ++i) { + const c = a + b; + + a = b; + + b = c; + } + + return b; + }; +} catch (error) { + console.log("Input must be a number"); +} + + diff --git a/tsconfig.json b/tsconfig.json index 3b8b39aa..9db2777d 100644 --- a/tsconfig.json +++ b/tsconfig.json @@ -17,4 +17,4 @@ "strictFunctionTypes": true, /* Visit https://aka.ms/tsconfig to read more about this file */ /* When assigning functions, check to ensure parameters and the return values are subtype-compatible. */ "skipLibCheck": true /* Skip type checking all .d.ts files. */ } -} +} \ No newline at end of file