isLuhnValid 🌶️🌶️🌶️
The Luhn algorithm is a checksum used to catch typos in card numbers. Write a method that takes one String parameter, digits, made up only of digit characters, and returns true if it passes the Luhn check, or false otherwise.
To perform the check:
1) Working from the RIGHTMOST digit and moving left, double every second digit (the 2nd, 4th, 6th, ... from the right); if a doubled value is greater than 9, subtract 9 from it.
2) After step 1, sum every digit. This value is called a checksum.
3) The number is valid exactly when that checksum is a multiple of 10.
Which direction do you need to walk the digits — left to right, or right to left? How would you track whether the current digit should be doubled?
A doubled digit can be at most 18 (9 * 2), so "subtract 9 if over 9" is the entire fix needed. It is equivalent to adding the doubled value's own two digits together, just faster to compute.