← Back to challenges

Digit Distance

JavaScriptHardnumbersarrays

Instructions

The digit distance between two numbers is the total value of the difference between each pair of digits.

To illustrate:

digitDistance(234, 489) ➞ 12
// Since |2 - 4| + |3 - 8| + |4 - 9| = 2 + 5 + 5

Create a function that returns the digit distance between two integers.

Examples

digitDistance(121, 599) ➞ 19

digitDistance(12, 12) ➞ 0

digitDistance(10, 20) ➞ 1

Notes

  • Both integers will be exactly the same length.
  • All digits in num2 have to be higher than their counterparts in num1.
javascript
Loading editor…
to run
Walks through the solution with reasoning and edge cases.
Next: Find the Largest Even Number