Skip to content
WANCODE
BM

Find the Array Bug

15 minIntermediateDebugging

The leaderboard shows 95, 9, 88 as the top three — but someone scored 100! And the "All scores" line is no longer in the order the scores came in. The function looks reasonable at first glance. Debug it.

JavaScript
topThree([88, 9, 100, 72, 95, 7])  // [100, 95, 88]
topThree([5, 40, 300])              // [300, 40, 5]
topThree([42])                      // [42]   (fewer than 3 is fine)
topThree([])                        // []

Expected results

Constraints

  • Return a new array of numbers, highest first. Duplicates count separately ([10, 10, 3] is valid).
  • Never modify the array that was passed in — other code still needs it in its original order.
  • Scores can be any numbers, including negatives.

topThree(scores) should return the three highest scores, highest first, without changing the original array. It returns the wrong scores and scrambles the input — find and fix the bugs.

Checks

  • Returns the real top three, highest first
  • Leaves the original array unchanged
  • Handles short arrays, duplicates and negatives
Preview

Console output from your code appears here.

Step 1 of 1