Back to Freecodecamp

Step 9

curriculum/challenges/english/blocks/learn-how-to-work-with-numbers-and-strings-by-implementing-the-luhn-algorithm/656879a66338b2a461d5d307.md

latest1.9 KB
Original Source

--description--

<!-- TODO: Find better places to split explanation up. -->

The Luhn algorithm is as follows:

  1. From the right to left, double the value of every second digit; if the product is greater than 9, sum the digits of the products.
  2. Take the sum of all the digits.
  3. If the sum of all the digits is a multiple of 10, then the number is valid; else it is not valid.

Assume an example of an account number "7992739871" that will have a check digit added, making it of the form 7992739871x:

markdown
Account number      7   9  9  2  7  3  9   8  7  1  x
Double every other  7  18  9  4  7  6  9  16  7  2  x
Sum 2-char digits   7   9  9  4  7  6  9   7  7  2  x

Replace the pass statement with a variable sum_of_odd_digits and a value of 0.

--hints--

You should have sum_of_odd_digits = 0 within the verify_card_number function.

js
({
    test: () => {
        const transformedCode = code.replace(/\r/g, "");
        const verify_card_number = __helpers.python.getDef("\n" + transformedCode, "verify_card_number");
        const { function_body } = verify_card_number;

        assert.match(function_body, /sum_of_odd_digits\s*=\s*0/);
    }
})

You should not have pass within the verify_card_number function.

js
({
    test: () => {
        const transformedCode = code.replace(/\r/g, "");
        const verify_card_number = __helpers.python.getDef("\n" + transformedCode, "verify_card_number");
        const { function_body } = verify_card_number;

        assert.notMatch(function_body, /pass/);
    }
})

--seed--

--seed-contents--

py
--fcc-editable-region--
def verify_card_number(card_number):
    pass

--fcc-editable-region--
def main():
    card_number = '4111-1111-4555-1142'
    card_translation = str.maketrans({'-': '', ' ': ''})
    translated_card_number = card_number.translate(card_translation)

    print(translated_card_number)

    verify_card_number(translated_card_number)

main()