JavaScript Math.min and Math.max — exercises and bug fixes
5 hands-on exercises and 5 find-the-bug challenges for the JavaScript Math.min and Math.max lesson. Every one is checked automatically in the browser — write the code, run it, and the tests tell you what still fails.
Exercises(5)
- 1
Find the largest of three
easy10 minReport the biggest of five, two and nine.
- 2
Find the largest in an array
easy10 minSpread the scores to find the highest.
- 3
Find the smallest in an array
easy10 minSpread the scores to find the lowest.
- 4
Clamp a value
easy10 minKeep 150 inside the range zero to one hundred.
- 5
Guard an empty array
medium10 minReport a message rather than spreading an empty array.
Find the bug(5)
- 1
Fix the array passed directly
easy8 minMath.max takes separate arguments, not an array.
- 2
Fix min used where max belongs
easy8 minThe highest score is wanted, not the lowest.
- 3
Fix the clamp the wrong way round
easy8 minWith the calls swapped a value below the range comes back as the maximum.
- 4
Fix the unguarded empty array
easy8 minSpreading an empty array gives -Infinity from max.
- 5
Fix sort used to find the largest
easy8 minThe default sort compares as text, so it puts 91 last rather than 100.
Ready to write the code?
The editor, the automatic checks and the worked solutions open up with an account.
Open the workspace