← Back to challenges

Maxie and Minnie

JavaScriptHardnumbersstringsarrays

Instructions

Maxie is the largest number that can be obtained by swapping two digits, Minnie is the smallest. Write a function that takes a number and returns an array of length 2, Maxie and Minnie. Leading zeros are not permitted.

Examples

maxMin(12340) ➞ [42310, 10342]

maxMin(98761) ➞ [98761, 18769]

maxMin(9000) ➞ [9000, 9000]
// Sometimes no swap needed.

maxMin(11321) ➞ [31121, 11123]

Notes

All inputs are positive, integers.

javascript
Loading editor…
to run
Walks through the solution with reasoning and edge cases.