Skip to main content

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. 1

    Find the largest of three

    easy10 min

    Report the biggest of five, two and nine.

  2. 2

    Find the largest in an array

    easy10 min

    Spread the scores to find the highest.

  3. 3

    Find the smallest in an array

    easy10 min

    Spread the scores to find the lowest.

  4. 4

    Clamp a value

    easy10 min

    Keep 150 inside the range zero to one hundred.

  5. 5

    Guard an empty array

    medium10 min

    Report a message rather than spreading an empty array.

Find the bug(5)

  1. 1

    Fix the array passed directly

    easy8 min

    Math.max takes separate arguments, not an array.

  2. 2

    Fix min used where max belongs

    easy8 min

    The highest score is wanted, not the lowest.

  3. 3

    Fix the clamp the wrong way round

    easy8 min

    With the calls swapped a value below the range comes back as the maximum.

  4. 4

    Fix the unguarded empty array

    easy8 min

    Spreading an empty array gives -Infinity from max.

  5. 5

    Fix sort used to find the largest

    easy8 min

    The 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