Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 11 additions & 4 deletions src/fib.ts
Original file line number Diff line number Diff line change
@@ -1,12 +1,19 @@
// src/fib.ts
// util function that computes the fibonacci numbers
module.exports = function fibonacci(n) {

/**
* Computes the n-th Fibonacci number.
* @param n - The index of the Fibonacci sequence (must be a non-negative integer)
* @returns The n-th Fibonacci number, or -1 if n is negative
*/
export function fibonacci(n: number): number {
if (n < 0) {
return -1;
} else if (n == 0) {
} else if (n === 0) {
return 0;
} else if (n == 1) {
} else if (n === 1) {
return 1;
}

return fibonacci(n - 1) + fibonacci(n - 2);
};
}
30 changes: 24 additions & 6 deletions src/fibRoute.ts
Original file line number Diff line number Diff line change
@@ -1,15 +1,33 @@
// Endpoint for querying the fibonacci numbers
// src/fibRoute.ts
import { fibonacci } from "./fib";

const fibonacci = require("./fib");
/**
* Endpoint for querying Fibonacci numbers
* @param req - Request object with a `params` property containing `num`
* @param res - Response object with a `send` method
*/
type Request = {
params: { num: string };
};

type Response = {
send: (message: string) => void;
};

export default (req, res) => {
export default (req: Request, res: Response) => {
const { num } = req.params;

const fibN = fibonacci(parseInt(num));
let result = `fibonacci(${num}) is ${fibN}`;
const parsedNum = parseInt(num, 10);

if (isNaN(parsedNum)) {
return res.send(`Invalid number: ${num}`);
}

const fibN = fibonacci(parsedNum);
let result = `fibonacci(${parsedNum}) is ${fibN}`;

if (fibN < 0) {
result = `fibonacci(${num}) is undefined`;
result = `fibonacci(${parsedNum}) is undefined`;
}

res.send(result);
Expand Down