Instruções
Teste de Luhn de números de cartão de crédito
O teste de Luhn é usado por algumas empresas de cartões de crédito para distinguir números válidos de cartão de crédito do que poderia ser uma seleção aleatória de dígitos.
Essas empresas que usam números de cartão de crédito que podem ser validados pelo teste de Luhn têm números que passam no teste a seguir:
<ol>
<li> Reverse the order of the digits in the number.</li>
<li> Take the first, third, ... and every other odd digit in the reversed digits and sum them to form the partial sum s1</li>
<li> Taking the second, fourth ... and every other even digit in the reversed digits:</li>
<ol>
<li>Multiply each digit by two and sum the digits if the answer is greater than nine to form partial sums for the even digits.</li>
<li>Sum the partial sums of the even digits to form s2.</li>
</ol>
<li>If s1 + s2 ends in zero then the original number is in the form of a valid credit card number as verified by the Luhn test.</li>
</ol>
Por exemplo, se o número avaliado for 49927398716:
Reverse the digits:
61789372994
Sum the odd digits:
6 + 7 + 9 + 7 + 9 + 4 = 42 = s1
The even digits:
1, 8, 3, 2, 9
Two times each even digit:
2, 16, 6, 4, 18
Sum the digits of each multiplication:
2, 7, 6, 4, 9
Sum the last:
2 + 7 + 6 + 4 + 9 = 28 = s2
s1 + s2 = 70 which ends in zero which means that 49927398716 passes the Luhn test.
O que fazer:
Escreva uma função que valide um número com o teste de Luhn. Retorne true se for um número válido. Caso contrário, retorne false.
Critérios de Aceitação:
Critérios de Aceitação:
Testes:
- `luhnTest` deve ser uma função.
- `luhnTest("4111111111111111")` deve retornar um booleano.
- `luhnTest("4111111111111111")` deve retornar `true`.
- `luhnTest("4111111111111112")` deve retornar `false`.
- `luhnTest("49927398716")` deve retornar `true`.
- `luhnTest("49927398717")` deve retornar `false`.
- `luhnTest("1234567812345678")` deve retornar `false`.
- `luhnTest("1234567812345670")` deve retornar `true`.
Console