๐ Interactive lesson ยท Grade 10 ยท about 75 minutes
Sort it, flip it, stamp it: arrays that obey you.
Line up players A to Z, flip them with a mirror, fall into the number-sorting trap and climb out with a compare function, and stamp labels on scores with map(). Then build an Arcade Leaderboard page whose buttons really sort and label the data.
- Grade 10 (ages 14-16)
- 8-step guided activity
- Live page with working buttons
- Free jump for teachers
- Free ยท no sign-up
Loading the lessonโฆ
What students learn
- Putting text in A to Z order with
sort(), and why capital letters come before small letters - Flipping an array with
reverse(), and whysort()must come first for Z to A - Why
[480, 95, 1200].sort()gives the wrong order, and how a compare function fixes it:a - bandb - a - Making a new array with
map()while the original stays the same, and why the function needsreturn - Connecting buttons to functions with
onclickand showing results withgetElementByIdandjoin()
The 6 modules
- The Sorting Shelf. Line up five players with sort(), then predict where capital and small letters land and see the letter codes that decide.
- The Mirror. Press reverse() and sort() in any order until the shelf reads Z to A, and find out why reverse() alone isn't enough.
- The Number Trap and the Compare Machine. Watch sort() put 1200 before 300, see why, then feed pairs into the a - b machine and sort the scores both ways.
- The Label Stamper. Choose a stamp, run map() and watch a new array appear while the original stays the same; then see what happens without return.
- Guided activity: Arcade Leaderboard. Eight checked steps in a live editor: three cards, the data, A to Z and Z to A, lowest and highest first, and labels with map(). The preview buttons really work.
- Wrap-up. Four rules to remember, a five-question quiz, an eye break and the Snack Shop after-class project with its boilerplate.
FAQ
JavaScript array sorting and mapping: common questions
How do you sort an array in JavaScript?
Call sort() on it, like names.sort(). It changes the original array and puts the items in text order: A to Z, with capital letters before small letters.
Why does sort() put 1200 before 300?
Without a compare function, sort() compares items as text, character by character. "1200" starts with 1 and "300" with 3, and 1 comes first. To sort numbers by value use nums.sort(function (a, b) { return a - b; }).
How do you sort numbers from highest to lowest?
Use a compare function that returns b - a: nums.sort(function (a, b) { return b - a; }). A positive result moves b in front of a.
How do you reverse an array in JavaScript?
Call reverse(): it flips the current order. It does not sort, so for Z to A call sort() first and then reverse().
What does map() do in JavaScript?
map() runs a function on every item and returns a new array of the results, for example scores.map(function (s) { return s + " pts"; }). The original array stays the same. If the function has no return, every new item is undefined.
Start a conversation
Questions about coding classes?
Tell us the learner's age and level and we'll suggest a starting point and confirm the format before anything is booked.
- Tell us the learnerโs age or grade and goals
- Weโll suggest a suitable class or learning path
- Weโll confirm format, timing and fees with a parent or guardian
Play it, don't just read it
Take a break and play
Free games that need no sign-up. Pick one and play for five minutes.
Pick a game
- Venn Diagrams and SetsSort people into two overlapping circles and count the overlap once.Play
- Mine LogicMinesweeper as a logic puzzle: open the safe squares, read the numbersโฆPlay
- Squares, Cubes & Roots TrainerLearn the squares from 1 to 30, the cubes, and square and cube roots,โฆPlay
- Integer IslandNavigate positive and negative numbers with addition, subtraction,โฆPlay