SOURCE

const orders = [
    {
        product: 'foo',
        quantity: 3,
        price: 2.00,
    },
    {
        product: 'bar',
        quantity: 1,
        price: 3.00,
    },
    {
        product: 'baz',
        quantity: 4,
        price: 5,
    },
];

/**
 * Case 1
 * 在 sum 函数里给出你的实现,期望 case1() 能够正确运行
 */
function sum() {
    // your code

}

function case1() {
    const quantity = sum(orders, order => order.quantity);
    const total = sum(orders, order => order.quantity * order.price);

    // test code
    expect(quantity).toBe(8)
    expect(total).toBe(29)
}

case1();










/**
 * Case 2
 * 在注释后给出你的实现,期望 case2() 能够正确运行
 */



function case2() {
    console.log('====================')
    const quantity = orders.sum(order => order.quantity);
    const total = orders.sum(order => order.quantity * order.price);

    expect(quantity).toBe(8)
    expect(total).toBe(29)
}

if (orders.sum) {
    case2();
}

function expect(value) {
    return {
        toBe: (expectedValue) => {
            console.log(`expect ${value} to be ${expectedValue}`, value === expectedValue ? '✅' : '❌')
        }
    }
}
console 命令行工具 X clear

                    
>
console