Back to Freecodecamp

Step 23

curriculum/challenges/english/blocks/learn-lambda-functions-by-building-an-expense-tracker/658238f7604f154ea9a23e1e.md

latest1.1 KB
Original Source

--description--

The sum() function returns the sum of the items in the iterable which is passed as its argument. You are going to use sum() together with map() and lambda functions to get the total amount of expenses.

For now, make a little test and modify your current print() call replacing the list() call with a call to the sum() function passing it the current map() call as the argument.

--hints--

You should print sum(map(test, [2, 3, 5, 8])).

js
({ test: () => assert.match(code, /^print\s*\(\s*sum\s*\(\s*map\s*\(\s*test\s*,\s*\[\s*2\s*,\s*3\s*,\s*5\s*,\s*8\s*\]\s*\)\s*\)\s*\)/m) })

--seed--

--seed-contents--

py
def add_expense(expenses, amount, category):
    expenses.append({'amount': amount, 'category': category})
    
def print_expenses(expenses):
    for expense in expenses:
        print(f'Amount: {expense["amount"]}, Category: {expense["category"]}')

def total_expenses(expenses):
    pass
    
--fcc-editable-region--
test = lambda x: x * 2
print(list(map(test, [2,3,5,8])))
--fcc-editable-region--

expenses = []