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
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
node_modules/
38 changes: 38 additions & 0 deletions src/utils/fibonacci.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
/**
* Author: Daniella Bertoldi and AI agents
* Date: 2 June 2026
* File: fibonacci.js
* Description: This script generates a Fibonacci sequence.
*/
'use strict';

// The fibonacci function generates a Fibonacci sequence up to a given number of terms
function fibonacci(n) {

// If the number is less than 0, throw an error
if (n < 0) {
throw new Error('Fibonacci sequence cannot be generated for negative numbers');
}

// If the number is 0, return an empty array
if (n === 0) {
return [];
}

// If the number is 1, return the first Fibonacci number
if (n === 1) {
return [0];
}

// Start the sequence with the first two Fibonacci numbers
const sequence = [0, 1];

// Add the next Fibonacci numbers until the sequence reaches n terms
for (let i = 2; i < n; i++) {
sequence.push(sequence[i - 1] + sequence[i - 2]);
}

return sequence;
}

module.exports = { fibonacci }; // Export the fibonacci function for use in other scripts
30 changes: 30 additions & 0 deletions test/utils/fibonacci.spec.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
/**
* Author: Daniella Bertoldi + AI Agents and Reserach
* Date: 2 June 2026
* File: fibonacci.spec.js
* Description: This script tests the fibonacci function.
*/
'use strict';

const { fibonacci } = require('../../src/utils/fibonacci');

// The describe() function is a test suite that contains one or more tests
describe('fibonacci.js', () => {

// The it() function is a test spec that contains one or more expectations
it('should generate a Fibonacci sequence with 1 term', () => {
const result = fibonacci(1);
expect(result).toEqual([0]);
});

// The it() function is a test spec that contains one or more expectations
it('should generate a Fibonacci sequence with 5 terms', () => {
const result = fibonacci(5);
expect(result).toEqual([0, 1, 1, 2, 3]);
});

// The it() function is a test spec that contains one or more expectations
it('should throw an error when generating a Fibonacci sequence for a negative number', () => {
expect(() => fibonacci(-1)).toThrow('Fibonacci sequence cannot be generated for negative numbers');
});
});