Skip to content
Draft
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
23 changes: 21 additions & 2 deletions demo.html
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,10 @@
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>FilterRange Function Demo</title>
<title>FilterRange and SortDescending Function Demo</title>
</head>
<body>
<h1>FilterRange Function Demo</h1>
<h1>FilterRange and SortDescending Function Demo</h1>
<p>Open the browser console to see the results.</p>

<script>
Expand All @@ -31,6 +31,25 @@ <h1>FilterRange Function Demo</h1>
// Log to console as well
console.log('Original array:', arr);
console.log('Filtered array:', filtered);

/**
* 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);
}

// Example from the problem statement
let arr2 = [5, 2, 1, -10, 8];
let sorted = sortDescending(arr2);

alert(`Original: ${arr2}, Sorted (descending): ${sorted}`); // Original: 5,2,1,-10,8, Sorted (descending): 8,5,2,1,-10

// Log to console as well
console.log('Original array for sorting:', arr2);
console.log('Sorted array (descending):', sorted);
</script>
</body>
</html>
23 changes: 23 additions & 0 deletions sortDescending.js
Original file line number Diff line number Diff line change
@@ -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]
}
42 changes: 42 additions & 0 deletions testSort.js
Original file line number Diff line number Diff line change
@@ -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();