diff --git a/demo.html b/demo.html index 320444a..7e8cb88 100644 --- a/demo.html +++ b/demo.html @@ -3,10 +3,10 @@ - FilterRange Function Demo + FilterRange and SortDescending Function Demo -

FilterRange Function Demo

+

FilterRange and SortDescending Function Demo

Open the browser console to see the results.

\ No newline at end of file diff --git a/sortDescending.js b/sortDescending.js new file mode 100644 index 0000000..77d1833 --- /dev/null +++ b/sortDescending.js @@ -0,0 +1,23 @@ +/** + * Sorts an array in descending order (내림차순) + * @param {number[]} arr - The input array + * @returns {number[]} A new array sorted in descending order + */ +function sortDescending(arr) { + return [...arr].sort((a, b) => b - a); +} + +// Export for use in other modules (if using Node.js) +if (typeof module !== 'undefined' && module.exports) { + module.exports = sortDescending; +} + +// Example usage as shown in the problem statement +if (typeof window === 'undefined') { + // Running in Node.js environment + let arr = [5, 2, 1, -10, 8]; + let sorted = sortDescending(arr); + + console.log('Original array:', arr); // Should show [5, 2, 1, -10, 8] (not modified) + console.log('Sorted array (descending):', sorted); // Should show [8, 5, 2, 1, -10] +} \ No newline at end of file diff --git a/testSort.js b/testSort.js new file mode 100644 index 0000000..e46305b --- /dev/null +++ b/testSort.js @@ -0,0 +1,42 @@ +// Test file for sortDescending function +const sortDescending = require('./sortDescending.js'); + +function test(description, actual, expected) { + const actualStr = JSON.stringify(actual); + const expectedStr = JSON.stringify(expected); + const passed = actualStr === expectedStr; + + console.log(`${passed ? '✅' : '❌'} ${description}`); + if (!passed) { + console.log(` Expected: ${expectedStr}`); + console.log(` Actual: ${actualStr}`); + } + return passed; +} + +function runSortTests() { + console.log('Running sortDescending tests...\n'); + + let allPassed = true; + + // Test case from the problem statement + let arr1 = [5, 2, 1, -10, 8]; + let sorted1 = sortDescending(arr1); + allPassed &= test('Problem statement test case [5, 2, 1, -10, 8]', sorted1, [8, 5, 2, 1, -10]); + allPassed &= test('Original array not modified', arr1, [5, 2, 1, -10, 8]); + + // Basic test cases + allPassed &= test('Basic positive numbers', sortDescending([3, 1, 4, 1, 5]), [5, 4, 3, 1, 1]); + allPassed &= test('Mixed positive and negative', sortDescending([3, -1, 4, -5, 0]), [4, 3, 0, -1, -5]); + allPassed &= test('All negative numbers', sortDescending([-3, -1, -4, -2]), [-1, -2, -3, -4]); + + // Essential edge cases + allPassed &= test('Empty array', sortDescending([]), []); + allPassed &= test('Single element', sortDescending([42]), [42]); + allPassed &= test('All same elements', sortDescending([7, 7, 7]), [7, 7, 7]); + + console.log(`\n${allPassed ? '🎉 All sort tests passed!' : '💥 Some sort tests failed!'}`); + return allPassed; +} + +runSortTests(); \ No newline at end of file