Back to Freecodecamp

Step 10

curriculum/challenges/english/blocks/learn-list-comprehension-by-building-a-case-converter-program/657f01ae9aea647b27402d3e.md

latest1.3 KB
Original Source

--description--

To wrap up the function, return the clean_snake_cased_string. This will complete the function and allow you to use it to convert strings from pascal or camel case to snake case.

Add a return statement at the end of the function to return the clean_snake_cased_string.

--hints--

You should add return clean_snake_cased_string at the end of the convert_to_snake_case function.

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

        assert.match(function_body, / +return\s+clean_snake_cased_string/);
    }
})

--seed--

--seed-contents--

py
def convert_to_snake_case(pascal_or_camel_cased_string):
    snake_cased_char_list = []
    for char in pascal_or_camel_cased_string:
        if char.isupper():
            converted_character = '_' + char.lower()
            snake_cased_char_list.append(converted_character)
        else:
            snake_cased_char_list.append(char)
    snake_cased_string = ''.join(snake_cased_char_list)
    clean_snake_cased_string = snake_cased_string.strip('_')
--fcc-editable-region--


--fcc-editable-region--