|You are developing an application for online transactions and you want to accept several credit cards. Card numbers, however, are usually long, so it's easy to make mistakes when typing them in. You want to create a method that will verify the numbers entered by users.
You know that the Luhn formula applies for all the acceptable card numbers.
The Luhn formula works as follows.
First, separate the individual digits of the credit card number. For example:
2 1 3 7 8
If there is an even number of digits, multiply each digit in an odd position by 2. Otherwise, multiply each digit in an even position by 2. Positions are 1-indexed, so the first digit is at position 1. The example number above contains an odd number of digits, so we multiply each digit in an even position by 2:
2 1 3 7 8 becomes
2 2 3 14 8
Note that the even positions refer to the original number so they don't change even when a 2-digit number appears.
Finally, take the sum of all the digits (for 2-digit numbers insert both the digits separately into the sum):
2+2+3+1+4+8 = 20
If the sum is a multiple of 10, the number is valid. Otherwise, it is invalid.
Given a String cardNumber containing the credit card number, return "VALID" if the card number is valid, or "INVALID" if it is invalid (all quotes for clarity).
|-||cardNumber will contain between 1 and 50 characters, inclusive.|
|-||Each character in cardNumber will be a digit ('0'-'9').|