Back to Freecodecamp

Step 19

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

latest1.7 KB
Original Source

--description--

Use a for loop to iterate over each digit in the odd_digits list. Move your print call from the previous step into the for loop, and change it to print each digit.

--hints--

You should use a for loop over odd_digits.

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, /for\s+\w+\s+in\s+odd_digits/);
    }
})

You should have --fcc-expected-- within the for loop.

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;
    
        // Get variable name used in for loop
        const for_loop_variable = function_body.match(/for\s+(\w+)\s+in\s+odd_digits/)?.[1];
        assert.exists(for_loop_variable);

        assert.equal(function_body.match(new RegExp(`print\\(\\s*${for_loop_variable}\\s*\\)`))?.[0], `print(${for_loop_variable})`);
    }
})

--seed--

--seed-contents--

py
--fcc-editable-region--
def verify_card_number(card_number):
    sum_of_odd_digits = 0
    card_number_reversed = card_number[::-1]
    odd_digits = card_number_reversed[::2]

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

    verify_card_number(translated_card_number)

main()