Skip to content
Exercise · Array methods · lesson 12 of 12

Exercise: top three scores

Write a function function topThree(scores) that returns a new array of the three largest numbers in the array scores, in descending order.

  • topThree([50, 90, 70, 80]) → [90, 80, 70]
  • topThree([5, 100, 20]) → [100, 20, 5]
  • topThree([7]) → [7]

Constraints: scores holds 0 to 10 integers from 0 to 1000; repeats stay in the result (topThree([9, 3, 9, 1]) → [9, 9, 3]); if there are fewer than three numbers, return all of them in descending order, and [] for an empty array; the array scores itself must not change.

Do not add extra imports unless the task says so explicitly.

top.js — javascript
javascript · 4 lines
Ctrl+Enter run · Esc leave editor

Subscribe to take the course

The course is free. Sign in with Telegram and subscribe to the channel to open every lesson.

Sign in another way