From f061f810d6aecb6996493f512441ceafa22c44e8 Mon Sep 17 00:00:00 2001 From: Mariea03 <110367002+Mariea03@users.noreply.github.com> Date: Wed, 14 Jan 2026 18:49:14 -0500 Subject: [PATCH] Add ascending array sort utility and tests --- src/utils/sortArray.js | 19 +++++++++++++++++++ test/utils/sortArray.spec.js | 27 +++++++++++++++++++++++++++ 2 files changed, 46 insertions(+) create mode 100644 src/utils/sortArray.js create mode 100644 test/utils/sortArray.spec.js diff --git a/src/utils/sortArray.js b/src/utils/sortArray.js new file mode 100644 index 0000000..15a67de --- /dev/null +++ b/src/utils/sortArray.js @@ -0,0 +1,19 @@ +/** + * Author: Mariea Nies + * Date: January 13 2026 + * File: sortArray.js + * Description: This function sorts an array of numbers in ascending order + */ +'use strict'; + +function sortArrayAscending(arr) { + // Validate input + if (!Array.isArray(arr)) { + throw new Error('Input must be an array'); + } + + // Return a NEW sorted array (do not mutate original) + return [...arr].sort((a, b) => a - b); +} + +module.exports = { sortArrayAscending }; \ No newline at end of file diff --git a/test/utils/sortArray.spec.js b/test/utils/sortArray.spec.js new file mode 100644 index 0000000..d9aa4c6 --- /dev/null +++ b/test/utils/sortArray.spec.js @@ -0,0 +1,27 @@ +/** + * Author: Mariea Nies + * Date: January 13 2026 + * File: sortArray.spec.js + * Description: This script tests the sort array function. + */ +'use strict' + +const { sortArrayAscending } = require('../../src/utils/sortArray'); + +describe('sortArray.js', () => { + it('should sort an array of numbers in ascending order', () => { + const result = sortArrayAscending([3, 1, 4, 2]); + expect(result).toEqual([1, 2, 3, 4]); + }); + + it('should handle an already sorted array', () => { + const result = sortArrayAscending([1, 2, 3, 4]); + expect(result).toEqual([1, 2, 3, 4]); + }); + + it('should handle negative numbers correctly', () => { + const result = sortArrayAscending([0, -5, 10, -2]); + expect(result).toEqual([-5, -2, 0, 10]); + }); + +}) \ No newline at end of file