Ready. Start with one instruction.
Loading the learning workspace…
FROM YOUR FIRST INSTRUCTION TO A WORKING APPLICATION
Learn JavaScript from scratch through small working examples, visual experiments and complete projects. No account or AI key required.
Predict → Experiment → Build → Test → Explain
Ready. Start with one instruction.
Loading the learning workspace…
The default sort compares string representations, so a numeric list needs an explicit comparison function. A negative comparison result places a before b; a positive result places b before a. The arrow callback syntax was introduced in Lesson 40. sort mutates its array, so this function sorts a shallow slice copy rather than changing the source. Test multiple digits and negative values, not only a list such as [1,2,3] that is already sorted.
function solve(numbers) {
const copy = numbers.slice();
copy.sort((a, b) => a - b);
return copy;
}
console.log(solve([10,2,1]));[1,2,10]
The default sort compares string representations, so a numeric list needs an explicit comparison function. A negative comparison result places a before b; a positive result places b before a. The arrow callback syntax was introduced in Lesson 40. sort mutates its array, so this function sorts a shallow slice copy rather than changing the source. Test multiple digits and negative values, not only a list such as [1,2,3] that is already sorted.
function solve(numbers) {Declare a function with a parameter receiving the actual argument supplied by each call. The definition does not execute its body yet.
const copy = numbers.slice();Create a shallow copy of the array so the following mutation does not change the source array.
copy.sort((a, b) => a - b);Sort the copy in place with the shown comparison callback. Negative callback results place the first value earlier.
return copy;Evaluate this expression, end the function and give the resulting value to its caller. It is not automatically printed.
}Close the current instruction block.
console.log(solve([10,2,1]));Evaluate the argument and display its value at this point. Quoted text is literal; an unquoted identifier reads its current binding.
function solve(numbers) {
const copy = numbers.slice();
copy.sort((a, b) => a - b);
return copy;
}
console.log(solve([]));[]
The default sort compares string representations, so a numeric list needs an explicit comparison function. A negative comparison result places a before b; a positive result places b before a. The arrow callback syntax was introduced in Lesson 40. sort mutates its array, so this function sorts a shallow slice copy rather than changing the source. Test multiple digits and negative values, not only a list such as [1,2,3] that is already sorted.
function solve(numbers) {Declare a function with a parameter receiving the actual argument supplied by each call. The definition does not execute its body yet.
const copy = numbers.slice();Create a shallow copy of the array so the following mutation does not change the source array.
copy.sort((a, b) => a - b);Sort the copy in place with the shown comparison callback. Negative callback results place the first value earlier.
return copy;Evaluate this expression, end the function and give the resulting value to its caller. It is not automatically printed.
}Close the current instruction block.
console.log(solve([]));Evaluate the argument and display its value at this point. Quoted text is literal; an unquoted identifier reads its current binding.
function solve(numbers) {
const copy = numbers.slice();
copy.sort((a, b) => a - b);
return copy;
}
console.log(solve([0,-2,10]));[-2,0,10]
The default sort compares string representations, so a numeric list needs an explicit comparison function. A negative comparison result places a before b; a positive result places b before a. The arrow callback syntax was introduced in Lesson 40. sort mutates its array, so this function sorts a shallow slice copy rather than changing the source. Test multiple digits and negative values, not only a list such as [1,2,3] that is already sorted.
function solve(numbers) {Declare a function with a parameter receiving the actual argument supplied by each call. The definition does not execute its body yet.
const copy = numbers.slice();Create a shallow copy of the array so the following mutation does not change the source array.
copy.sort((a, b) => a - b);Sort the copy in place with the shown comparison callback. Negative callback results place the first value earlier.
return copy;Evaluate this expression, end the function and give the resulting value to its caller. It is not automatically printed.
}Close the current instruction block.
console.log(solve([0,-2,10]));Evaluate the argument and display its value at this point. Quoted text is literal; an unquoted identifier reads its current binding.
Repair the intentionally changed operation so the example produces the exact expected result. Then try the extension without copying the answer.
function solve(numbers) {
const copy = numbers.slice();
copy.sort((a, b) => b - a);
return copy;
}
console.log(solve([10,2,1]));function solve(numbers) {
const copy = numbers.slice();
copy.sort((a, b) => a - b);
return copy;
}
console.log(solve([10,2,1]));Choose a comparator matching the intended order and copy first when source data must remain unchanged.
Which operation changed the result, why did it work, and what would you test next?
Use a different value that could expose a boundary or mistake. Predict the result first, run it, and explain how the concept would help in a small real application.
Related: Previous lesson Next lesson
100 lessons across 20 modules, covering variables, operators, strings, conditions, loops, functions, scope, arrays, objects, DOM, events, state, testing, asynchronous JavaScript and application development.