• in
InterCourses
CoursesBlogs
0
← Introduction to JavaScript
○What is JavaScript?○Your First JavaScript Code○Data Types○Type Checking
○Declaring Variables○let and const in Practice○Operators○Type Conversion
○Conditionals○Switch Statement○For Loops○While Loop○Break and Continue
○Defining Functions○Arrow Functions○Scope○Closures○Project - Calculator○Function Expressions
○Arrays○Array Mutation Methods○Array Search and Slice○Array Iteration Methods○Multidimensional Arrays○Destructuring●Array Reverse and Sort○Array Fill○Array Find and Includes○Array Flat and FlatMap○Array Reduce, Some, and Every
○Objects○Object Methods and Destructuring○JSON○Math and Date○Date Basics
○02 Template Literals○String Methods○Regular Expressions
○Higher-Order Functions○Callbacks○map, filter, and reduce○Recursion○Project - Data Transform Pipeline
○OOP Introduction○Classes○Inheritance○Encapsulation
○Asynchronous JavaScript○Promises and Async Await○Async API Simulation

Array Reverse and Sort

Sorting and reversing are two common array operations that beginners often use together. This lesson keeps the data small and the output explicit so the order is easy to verify.

Example

javascript
const numbers = [3, 1, 4, 2];
const sortedNumbers = [...numbers].sort((a, b) => a - b);
const reversedNumbers = [...sortedNumbers].reverse();

Your Task

  1. Create sortedNumbers from [3, 1, 4, 2] so it becomes [1, 2, 3, 4].
  2. Create reversedNumbers from the sorted array and display [1,2,3,4] | [4,3,2,1] in #output.
Hint 1
1 / 2
HINT 1

Use a copy like [...numbers] before sorting so you do not lose the original array.

Loading editor…
READY
intercourses
javascript