give me a fibonnaci function in ts
- User
give me a fibonnaci function in ts
Assistant
Sure! Here's an example implementation of a Fibonacci function in TypeScript:
function fibonacci(n: number): number { if (n === 0) { return 0; } else if (n === 1) { return 1; } else { return fibonacci(n - 1) + fibonacci(n - 2); } }This function takes a number
nas input and returns thenth Fibonacci number. Ifnis 0 or 1, it returns the corresponding value in the Fibonacci sequence (0 and 1, respectively). Otherwise, it recursively calls thefibonaccifunction withn-1andn-2as inputs and adds the results together to get thenth Fibonacci number.You can use this function in your TypeScript code like this:
console.log(fibonacci(10)); // Output: 55This will output
55, which is the 10th Fibonacci number.
Other models, same or similar prompt
Responses were produced in different, uncontrolled contexts (system prompts, settings, prior turns). This is not a benchmark.