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
13 changes: 11 additions & 2 deletions src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -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
main(); // Call the main function to calculate the factorial of 5 and sort the array ascendingly
36 changes: 36 additions & 0 deletions src/utils/sortAscArray.js
Original file line number Diff line number Diff line change
@@ -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
32 changes: 32 additions & 0 deletions test/utils/sortAscArray.spec.js
Original file line number Diff line number Diff line change
@@ -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");
});
});