diff --git a/src/index.js b/src/index.js index ca8a5cf..53400fe 100644 --- a/src/index.js +++ b/src/index.js @@ -3,15 +3,24 @@ * Date: 13 September 2024 * File: index.js * Description: This script is the main entry point for the + * */ // Import the factorial function from the factorial.js file const { factorial } = require('./utils/factorial'); -// The main() function calculates the factorial of 5 and logs the result to the console +//Import the sortAscArray function from sortAscArray.js file +const { sortAscArray } = require('./utils/sortAscArray'); + +// The main() function calculates the factorial of 5 and logs the result to the console and sort the array ascendingly and log to console async function main () { const result = factorial(5); // Call the factorial function with the value of 5 console.log('factorial of 5 is', result); // Log the result to the console + + + const myArray = [5, 8, 1, 2]; //array to sort ascendingly + const sortedArray = sortAscArray(myArray); //sorted array + console.log('array sorted ascendingly: ', sortedArray); //log to console } -main(); // Call the main function to calculate the factorial of 5 \ No newline at end of file +main(); // Call the main function to calculate the factorial of 5 and sort the array ascendingly \ No newline at end of file diff --git a/src/utils/sortAscArray.js b/src/utils/sortAscArray.js new file mode 100644 index 0000000..4f94a4c --- /dev/null +++ b/src/utils/sortAscArray.js @@ -0,0 +1,36 @@ +/** + * Author: Shannon Kueneke + * Date: June 2, 2026 + * File: sortAscArray.js + * Description: This script sorts a given array in ascending order + */ +"use strict"; + +// The sortAscArray function sorts a given array in ascending order +function sortAscArray(arr) { + //if the value passed is not an array, throw an error + if (!Array.isArray(arr)) { + throw new Error("Passed value is not an array"); + } + //checks the type of values contained in passed array and adds type to new array + let typeArray = []; + arr.forEach((value) => { + if (!typeArray.includes(typeof value)) { + typeArray.push(typeof value); + } + }); + + //if the values are mixed type or strings they must be compared by their string value + if (typeArray.length > 1) { + //convert all values to strings to then compare values to sort + return arr.sort((a, b) => String(a).localeCompare(String(b))); + } + + //if the values in the array passed are all numbers + if (typeArray.length === 1 && typeArray[0] === 'number') { + return arr.sort((a, b) => a - b); + } +} + + +module.exports = { sortAscArray }; // Export the sortAscArray function for use in other scripts diff --git a/test/utils/sortAscArray.spec.js b/test/utils/sortAscArray.spec.js new file mode 100644 index 0000000..f007f22 --- /dev/null +++ b/test/utils/sortAscArray.spec.js @@ -0,0 +1,32 @@ +/** + * Author: Shannon Kueneke + * Date: June 2, 2026 + * File: sortAscArray.spec.js + * Description: This script tests the sortAscArray function. + */ +"use strict"; + +//Import the sortAscArray function from sortAscArray.js file +const { sortAscArray } = require("../../src/utils/sortAscArray"); + +// The describe() function is a test suite that contains one or more tests +describe("sortAscArray.js", () => { + // The it() function is a test spec that contains one or more expectations + it("should sort the given array in ascending order", () => { + const result = sortAscArray([5, 10, 1, 2]); //call the sortAscArray function with value of [5, 10, 1, 2] + expect(result).toEqual([1, 2, 5, 10]); + }); + + // The it() function is a test spec that contains one or more expectations + it("should convert to and compare all array values as strings if array does not contain only numbers before sorting values ascendingly in their original type", () => { + const result = sortAscArray([500, "apple", 1, -2, 1000, true]); //call the sortAscArray function with value of [500, "apple", 1, -2, 1000, true] + expect(result).toEqual([-2, 1, 1000, 500, 'apple', true]); + }); + + // The it() function is a test spec that contains one or more expectations + it("should throw an error if passed value is not an array", () => { + // Call the sortAscArray function passing a string and expect it to throw an error + expect(() => sortAscArray("hello")).toThrow("Passed value is not an array"); + }); +}); +