Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merge fix-plus-operation-type-check: Improve Fibonacci function with iterative approach and enhanced error handling #47

Open
wants to merge 26 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
26 commits
Select commit Hold shift + click to select a range
7e01fef
Fix plus operation type check in fibonacci function
fdounis Sep 1, 2024
2c97b90
fixed unsafe assignment issue in issue 2
T7alabdullah Sep 1, 2024
3cd2d38
Fix plus operation type check in fibonacci function v2
fdounis Sep 1, 2024
ce3bed4
Merge pull request #3 from T7alabdullah/fix-plus-operation-type-check
fdounis Sep 1, 2024
39ec54e
fixed unsafe assignment issue in issue 2 again
T7alabdullah Sep 1, 2024
68cae68
Improve Fibonacci function: switch to iterative approach and enhance …
fdounis Sep 1, 2024
cd839be
fixed unsafe assignment issue in issue 2 again
T7alabdullah Sep 1, 2024
4986e92
Merge pull request #5 from T7alabdullah/fix-plus-operation-type-check
fdounis Sep 1, 2024
9bd9998
Clearly define variable types
fdounis Sep 1, 2024
07c3fc3
fixed unsafe assignment issue in issue 2 again
T7alabdullah Sep 1, 2024
c38ce08
Merge pull request #6 from T7alabdullah/fix-plus-operation-type-check
fdounis Sep 1, 2024
4cc98a3
Clearly define variable types
fdounis Sep 1, 2024
87f4956
Merge pull request #7 from T7alabdullah/fix-plus-operation-type-check
fdounis Sep 1, 2024
3780907
fixed unsafe assignment issue in issue 2 in relation to .ts file
T7alabdullah Sep 1, 2024
8c753dd
Merge pull request #4 from T7alabdullah/fix-unsafe-assignment-issue-t…
T7alabdullah Sep 1, 2024
a4ab1d1
Merge pull request #8 from T7alabdullah/main
T7alabdullah Sep 1, 2024
81858b8
fixed unsafe assignment issue in issue 2 in relation to .ts file
T7alabdullah Sep 1, 2024
d4817d0
Fix eslint errors by removing redundant type annotations and ensuring…
T7alabdullah Sep 1, 2024
1d8b00a
Fix eslint errors by removing redundant type annotations and ensuring…
T7alabdullah Sep 1, 2024
767dc6b
Merge pull request #9 from T7alabdullah/fix-unsafe-assignment-issue-t…
T7alabdullah Sep 1, 2024
c47987c
Fix negative number and range errors
T7alabdullah Sep 1, 2024
ef915a0
Merge pull request #10 from T7alabdullah/fix-unsafe-assignment-issue-…
T7alabdullah Sep 1, 2024
e797c5b
Fixing result syntax with let
T7alabdullah Sep 1, 2024
dc49243
Merge pull request #11 from T7alabdullah/fix-unsafe-assignment-issue-…
T7alabdullah Sep 1, 2024
e386472
test
fdounis Sep 1, 2024
51dc8fb
Merge pull request #12 from T7alabdullah/fix-plus-operation-type-check
fdounis Sep 1, 2024
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
27 changes: 21 additions & 6 deletions src/fib.ts
Original file line number Diff line number Diff line change
@@ -1,12 +1,27 @@
// util function that computes the fibonacci numbers
module.exports = function fibonacci(n) {
function fibonacci(n: number): number {
if (typeof n !== 'number' || isNaN(n)) {
throw new TypeError('Input must be a valid number');
}

if (n < 0) {
return -1;
} else if (n == 0) {
throw new RangeError('Input must be a non-negative integer');
} else if (n === 0) {
return 0;
} else if (n == 1) {
} else if (n === 1) {
return 1;
}

return fibonacci(n - 1) + fibonacci(n - 2);
};
// Removed redundant type annotations
let a = 0;
let b = 1;
let temp: number;

for (let i = 2; i <= n; i++) {
temp = a + b;
a = b;
b = temp;
}
return b;
}
export default fibonacci;
26 changes: 17 additions & 9 deletions src/fibRoute.ts
Original file line number Diff line number Diff line change
@@ -1,16 +1,24 @@
// Endpoint for querying the fibonacci numbers
import { Request, Response } from 'express';
import fibonacci from './fib';

const fibonacci = require("./fib");
export default (req: Request, res: Response): void => {
const numString = req.params.num as unknown as string; // Ensure it's treated as a string
const num = parseInt(numString, 10); // Safely parse num as an integer

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

const fibN = fibonacci(parseInt(num));
let result = `fibonacci(${num}) is ${fibN}`;
// Check if num is a valid number after parsing
if (isNaN(num)) {
res.status(400).send('Invalid input. Please provide a valid number.');
return;
}

if (fibN < 0) {
result = `fibonacci(${num}) is undefined`;
// Check if the number is negative
if (num < 0) {
res.send(`fibonacci(${num}) is undefined`);
return;
}

const fibN = fibonacci(num); // Compute the Fibonacci number safely
const result = `fibonacci(${num}) is ${fibN}`; // Use const instead of let

res.send(result);
};