Fix numeric sort in `sortScores`
easy
sort(). It looked fine in a quick demo with single-digit values, then broke the moment scores like 10 and 100 showed up. Fix the bug so numbers sort numerically in ascending order while keeping the original input unchanged.Solution idea
export default function sortScores(scores){
return scores.slice().sort((a, b) => a - b);
}
Examples
sortScores([10, 2, 30, 4]) // [2, 4, 10, 30]
sortScores([1, 100, 20, 3]) // [1, 3, 20, 100]Solution
Overview
export default function sortScores(scores){
return scores.slice().sort((a, b) => a - b);
}
Similar questions
Guides
Preparing for interviews? Use Frontend Coding Challenges first, then move into a concrete Study Plan before targeted Company Prep.
Open frontend interview questionsBrowse JavaScript interview questionsOpen Essential 60Open Machine Coding HubOpen Frontend Coding ChallengesOpen System DesignOpen Interview Prep GuideOpen System Design BlueprintOpen Framework Prep PathsOpen Study PlansOpen JavaScript mastery study planOpen Company PrepOpen JavaScript Framework Prep Guide
Built in public
The code behind FrontendAtlas lives on GitHub. A star helps other frontend engineers find it.