๐Ÿƒ 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
ShareWhatsApp

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 why sort() must come first for Z to A
  • Why [480, 95, 1200].sort() gives the wrong order, and how a compare function fixes it: a - b and b - a
  • Making a new array with map() while the original stays the same, and why the function needs return
  • Connecting buttons to functions with onclick and showing results with getElementById and join()

The 6 modules

  1. The Sorting Shelf. Line up five players with sort(), then predict where capital and small letters land and see the letter codes that decide.
  2. 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.
  3. 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.
  4. 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.
  5. 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.
  6. 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

For a childโ€™s class, please have a parent or guardian send the enquiry. Avoid sharing passwords or sensitive personal details.

Weโ€™ll use your details only to respond to this request.

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.

All free games โ†’
Chat on WhatsApp
Esc

Try