Skip to content
Draft
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
24 changes: 24 additions & 0 deletions factorial.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
const MAX_FACTORIAL_INPUT = 18; // factorial(18) is within JavaScript's safe integer range

/**
* Computes the factorial of a non-negative integer.
* Supports inputs from 0 to 18 (factorial values within Number.MAX_SAFE_INTEGER).
* @param {number} n - A non-negative integer (0–18)
* @returns {number} The factorial of n
*/
function factorial(n) {
if (typeof n !== 'number' || !Number.isInteger(n) || n < 0) {
throw new Error('Input must be a non-negative integer');
}
if (n > MAX_FACTORIAL_INPUT) {
throw new Error(`Input must not exceed ${MAX_FACTORIAL_INPUT} to ensure precise integer results`);
}
if (n === 0 || n === 1) return 1;
let result = 1;
for (let i = 2; i <= n; i++) {
result *= i;
}
return result;
}

module.exports = { factorial };
15 changes: 15 additions & 0 deletions index.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
const express = require('express');
const bodyParser = require('body-parser');
const db = require('./database');
const { factorial } = require('./factorial');

const app = express();

Expand Down Expand Up @@ -258,6 +259,20 @@ app.delete('/api/tasks/:id', (req, res) => {
});
});

// Factorial Route
app.get('/api/factorial/:n', (req, res) => {
const n = parseInt(req.params.n, 10);
if (isNaN(n) || n < 0 || String(n) !== req.params.n) {
return res.status(400).json({ error: 'Input must be a non-negative integer' });
}
try {
const result = factorial(n);
res.json({ n, result });
} catch (err) {
res.status(400).json({ error: err.message });
}
});

// Error handling middleware
app.use((err, req, res, next) => {
console.error('Unhandled error:', err);
Expand Down
112 changes: 112 additions & 0 deletions tests/factorial.test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,112 @@
const request = require('supertest');
const express = require('express');
const bodyParser = require('body-parser');
const { factorial } = require('../factorial');

// Build a minimal app for testing the factorial route
const app = express();
app.use(bodyParser.json());

app.get('/api/factorial/:n', (req, res) => {
const n = parseInt(req.params.n, 10);
if (isNaN(n) || n < 0 || String(n) !== req.params.n) {
return res.status(400).json({ error: 'Input must be a non-negative integer' });
}
try {
const result = factorial(n);
res.json({ n, result });
} catch (err) {
res.status(400).json({ error: err.message });
}
});

describe('Factorial utility', () => {
test('factorial(0) should return 1', () => {
expect(factorial(0)).toBe(1);
});

test('factorial(1) should return 1', () => {
expect(factorial(1)).toBe(1);
});

test('factorial(5) should return 120', () => {
expect(factorial(5)).toBe(120);
});

test('factorial(10) should return 3628800', () => {
expect(factorial(10)).toBe(3628800);
});

test('should throw for negative numbers', () => {
expect(() => factorial(-1)).toThrow('Input must be a non-negative integer');
});

test('should throw for non-integer input', () => {
expect(() => factorial(3.5)).toThrow('Input must be a non-negative integer');
});

test('should throw for non-number input', () => {
expect(() => factorial('abc')).toThrow('Input must be a non-negative integer');
});

test('should throw for input exceeding maximum safe value', () => {
expect(() => factorial(19)).toThrow('Input must not exceed 18');
});
});

describe('GET /api/factorial/:n', () => {
test('should return factorial of 0', (done) => {
request(app)
.get('/api/factorial/0')
.expect(200)
.end((err, res) => {
if (err) return done(err);
expect(res.body).toEqual({ n: 0, result: 1 });
done();
});
});

test('should return factorial of 5', (done) => {
request(app)
.get('/api/factorial/5')
.expect(200)
.end((err, res) => {
if (err) return done(err);
expect(res.body).toEqual({ n: 5, result: 120 });
done();
});
});

test('should return factorial of 10', (done) => {
request(app)
.get('/api/factorial/10')
.expect(200)
.end((err, res) => {
if (err) return done(err);
expect(res.body).toEqual({ n: 10, result: 3628800 });
done();
});
});

test('should return 400 for negative number', (done) => {
request(app)
.get('/api/factorial/-1')
.expect(400)
.end((err, res) => {
if (err) return done(err);
expect(res.body.error).toBeDefined();
done();
});
});

test('should return 400 for non-integer input', (done) => {
request(app)
.get('/api/factorial/abc')
.expect(400)
.end((err, res) => {
if (err) return done(err);
expect(res.body.error).toBeDefined();
done();
});
});
});