1. slice copies
slice returns a shallow copy of the [1, 3) range and leaves arr unchanged.
const head = arr.slice(1, 3);Typorithm · medium
Step 1 of 8 · intro card
Array Methods
slice returns a copy of a range and never mutates; splice mutates the array in place to remove or insert elements and returns what it removed; sort mutates in place and needs a numeric comparator because its default ordering is lexicographic (string-based).
slice(start, end) returns a shallow copy of the [start, end) range and leaves the original array untouched — it is the safe, non-mutating way to take a sub-array.
splice(start, count) mutates the array in place: it removes count elements starting at start (and can insert new ones), returning an array of the removed elements.
sort also mutates in place. Its default comparison is lexicographic — it converts elements to strings — so [30, 10, 20, 40] would sort to [10, 20, 30, 40] only if you pass a numeric comparator like (a, b) => a - b.
slice copies a range and does not mutate.
splice mutates in place and returns removed elements.
sort mutates and defaults to lexicographic order — pass a comparator.
const arr = [30, 10, 20, 40];
const head = arr.slice(1, 3);
const removed = arr.splice(1, 2);
arr.sort((a, b) => a - b);Connect each part of the code to the concept before you type.
slice returns a shallow copy of the [1, 3) range and leaves arr unchanged.
const head = arr.slice(1, 3);splice mutates arr in place, removing 2 elements from index 1 and returning the removed ones.
const removed = arr.splice(1, 2);sort reorders arr in place; the (a, b) => a - b comparator sorts numerically.
arr.sort((a, b) => a - b);Without a comparator, sort compares elements as strings, so a numeric comparator is required for numbers.
(a, b) => a - bRead the concept map and follow the code breakdown.
Type the implementation, repair mistakes and work through every recall exercise.
Use the final check without hints; revisit weak concepts in short practice sessions.