Skip to content
Merged
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
8 changes: 4 additions & 4 deletions src/fib.ts
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
// util function that computes the fibonacci numbers
module.exports = function fibonacci(n) {
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);
};
}
31 changes: 23 additions & 8 deletions src/fibRoute.ts
Original file line number Diff line number Diff line change
@@ -1,16 +1,31 @@
// Endpoint for querying the fibonacci numbers

const fibonacci = require("./fib");
import { fibonacci } from "./fib";
Copy link
Copy Markdown
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Good way of fixing the error of the import that made the lint run


export default (req, res) => {
const { num } = req.params;
// Import types
type Req = { params: { num?: string } };
type Res = { status: (code: number) => Res; send: (msg: string) => void };

const fibN = fibonacci(parseInt(num));
let result = `fibonacci(${num}) is ${fibN}`;
export default function fibRoute(req: Req, res: Res): void {
const numParam = req.params.num; // no more unsafe access
if (!(typeof numParam === "string" )) {
res.status(401).send("Missing 'num'");
return;
}

if (fibN < 0) {
result = `fibonacci(${num}) is undefined`;
const n = parseInt(numParam, 10); // turn into base10 int
if (Number.isNaN(n)) {
res.status(402).send(`Invalid number: "${numParam}"`);
return;
}

const retVal = fibonacci(n);
let result = "";
// serialize
if (retVal < 0) {
result = `fibonacci(${n}) is undefined`;
} else {
result = `fibonacci(${n}) is ${retVal}`;
}
res.send(result);
};
}