前端练习2——(小费/消费计算,求平均数)

作业1

const bills = [48, 124, 268] //三笔消费
const tips = [];
const payments = [];
const tipCaculator = () => {
    for (let i = 0; i < bills.length; i++) {
        const x = bills[i]
        if (x < 50) {
            const tip = x * 0.2
            const pay = x + tip
            tips.push(tip)
            payments.push(pay)
        }
        if (x >= 50 & x <= 200) {
            const tip = x * 0.15
            const pay = x + tip
            tips.push(tip)
            payments.push(pay)
        } else if (x > 200) {
            const tip = x * 0.1
            const pay = x + tip
            tips.push(tip)
            payments.push(pay)
        }
    }
    console.log("三笔消费的小费分别为:", tips) //小费
    console.log("三笔消费的小费分别为:", payments) // console.log("三笔消费的小费分别为:",payment)//总消费
}
tipCaculator()

作业2

//原函数
function mean(a, b, c) {
    return (a + b + c) / 3;
}

//要求1:要求任意个数的数字的平均值,求改进后代码
const mean1 = (...numbers) => {
    let l = numbers.length;
    let sum = 0;
    for (const num of numbers) {
        sum += num;
    }
    let Avg = sum / l;
    console.log(Avg);
}
mean1(1, 2, 3, 4, 5, 6, 7, 8, 9, 10);

//要求2:使用reduce()函数精简
const mean2 = (numbers) => {
    let l = numbers.length;
    let Avg = numbers.reduce((numbers, sum) => numbers + sum) / l;
    return Avg
}
console.log(mean2([1, 2, 3, 4, 5, 6, 7, 8, 9, 10]));

//要求3:只对数组中的偶数品平均数
const asd = []; //新数组存储偶数
const mean2 = () => {
    let l = asd.length;
    let Avg = asd.reduce((asd, sum) => asd + sum) / l;
    console.log(Avg)
} //求平均值
const mean3 = (arr1, callback) => {
    const arr2 = arr1.map((sum) => {
        if (sum % 2 == 0) {
            return sum
        }
    }) //取偶数
    // const asd = [];
    arr2.forEach(
        item => {
            if (item) {
                asd.push(item)
            }
        }
    ) //偶数组成新的数组
    if (typeof (callback) === "function") {
        callback()
    } //回调mean2()
}
mean3([1, 2, 3, 4, 5, 6, 7, 8, 9, 10], mean2)

//finished
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值