Back to Freecodecamp

Step 4

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

latest1.5 KB
Original Source

--description--

Defining the translation does not in itself translate the string. The translate method must be called on the string to be translated with the translation table as an argument:

py
my_string = "tamperlot"
translation_table = str.maketrans({'t': 'c', 'l': 'b'})
translated_string = my_string.translate(translation_table)

Create a variable called translated_card_number and assign it the result of calling the translate method on card_number with card_translation as an argument.

--hints--

You should create a translated_card_number variable within main.

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

        assert.match(function_body, / +translated_card_number\s*=/);
    }
})

You should assign translated_card_number a value of card_number.translate(card_translation).

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

        assert.match(function_body, / +translated_card_number\s*=\s*card_number\.translate\(\s*card_translation\s*\)/);
    }
})

--seed--

--seed-contents--

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