File tree Expand file tree Collapse file tree
Expand file tree Collapse file tree Original file line number Diff line number Diff line change 11// src/fib.ts
2- export default function fibonacci ( n : number ) : number {
2+ export function fibonacci ( n : number ) : number {
33 if ( n < 0 ) return - 1 ;
44 if ( n === 0 ) return 0 ;
55 if ( n === 1 ) return 1 ;
66
77 let a = 0 ;
88 let b = 1 ;
99 for ( let i = 2 ; i <= n ; i ++ ) {
10- const next = a + b ; // both are number
10+ const next = a + b ;
1111 a = b ;
1212 b = next ;
1313 }
Original file line number Diff line number Diff line change 1- // Endpoint for querying the fibonacci numbers
1+ // src/fibRoute.ts
2+ import type { Request , Response } from "express" ;
3+ import { fibonacci } from "./fib" ;
24
3- const fibonacci = require ( "./fib" ) ;
5+ export default function fibRoute ( req : Request , res : Response ) : void {
6+ const numParam : string | undefined = req . params ?. num ;
47
5- export default ( req , res ) => {
6- const { num } = req . params ;
7-
8- const fibN = fibonacci ( parseInt ( num ) ) ;
9- let result = `fibonacci(${ num } ) is ${ fibN } ` ;
8+ if ( typeof numParam !== "string" ) {
9+ res . status ( 400 ) . send ( "Missing route parameter 'num'" ) ;
10+ return ;
11+ }
1012
11- if ( fibN < 0 ) {
12- result = `fibonacci(${ num } ) is undefined` ;
13+ const n = Number . parseInt ( numParam , 10 ) ;
14+ if ( Number . isNaN ( n ) ) {
15+ res . status ( 400 ) . send ( `Invalid number: "${ numParam } "` ) ;
16+ return ;
1317 }
1418
19+ const fibN = fibonacci ( n ) ;
20+ const result =
21+ fibN < 0
22+ ? `fibonacci(${ n } ) is undefined`
23+ : `fibonacci(${ n } ) is ${ fibN } ` ;
24+
1525 res . send ( result ) ;
16- } ;
26+ }
You can’t perform that action at this time.
0 commit comments