diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..c2658d7 --- /dev/null +++ b/.gitignore @@ -0,0 +1 @@ +node_modules/ diff --git a/src/utils/fibonacci.js b/src/utils/fibonacci.js new file mode 100644 index 0000000..aa76fa6 --- /dev/null +++ b/src/utils/fibonacci.js @@ -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 \ No newline at end of file diff --git a/test/utils/fibonacci.spec.js b/test/utils/fibonacci.spec.js new file mode 100644 index 0000000..19b0405 --- /dev/null +++ b/test/utils/fibonacci.spec.js @@ -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'); + }); +});