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/
2 changes: 1 addition & 1 deletion package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

28 changes: 28 additions & 0 deletions src/utils/gcd.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
/**
* Author: Aisha Keller
* Date: 2 June 2026
* File: gcd.js
* Description: This script calculates the greatest common divisor of two numbers
*/

'use strict';

// The gcd function calculates the greates common divisor of two numbers
function gcd(a, b) {

// If either number is negative, throw an error
if (a < 0 || b < 0) {
throw new Error('GCD of negative numbers is not defined');
}

// Use the Euclidean alorithm to calculate the GCD
while (b !== 0) {
let temp = b;
b = a % b;
a = temp;
}

return a; // Return the GCD
}

module.exports = { gcd }; // Export the gcd function for use in other scripts
32 changes: 32 additions & 0 deletions test/utils/gcd.spec.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
/**
* Author: Aisha Keller
* Date: 2 June 2026
* File: gcd.spec.js
* Description: This script tests the gcd function.
*/

'use strict';

const { gcd } = require('../../src/utils/gcd'); // Import the gcd function from the gcd.js file

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

// Test 1: The it() should calculate the GCD of 12 and 8
it('should calculate the GCD of 12 and 8', () => {
const result = gcd(12, 8); // Call the gcd function with the values of 12 and 8;
expect(result).toBe(4); // The expected result is 4
});

// Test 2: The it() should calculate the GCD of 100 and 25
it('should calculate the GCD of 100 and 25', () => {
const result = gcd(100, 25); // Call the gcd function with the values of 100 and 25
expect(result).toBe(25); // The expected result is 25
});

// Test 3: The it() should throw an error when calculating the GCD of a negative number
it('should throw an error when calculating the GCD of a negative number', () => {
// Call the gcd function with the values of -12 and 8 and expect it to throw an error
expect(() => gcd(-1, 5)).toThrow('GCD of negative numbers is not defined');
});
});