Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
4.1k views
in Technique[技术] by (71.8m points)

计算大胃王小明吃苹果数量.(根据时间戳得到区间,然后算出区间值之和)

计算大胃王小明吃苹果数量:

已知小明2020-08-16至2020-09-15的吃苹果的数据如下:

*// steps[].step 苹果量
// steps[].timestamp 时间戳*
    const steps = [{
        step: 1753,
        timestamp: 1597507200000
    }, {
        step: 1637,
        timestamp: 1597593600000
    }, {
        step: 6216,
        timestamp: 1597680000000
    }, {
        step: 6466,
        timestamp: 1597766400000
    }, {
        step: 12998,
        timestamp: 1597852800000
    }, {
        step: 7731,
        timestamp: 1597939200000
    }, {
        step: 7115,
        timestamp: 1598025600000
    }, {
        step: 3603,
        timestamp: 1598112000000
    }, {
        step: 1659,
        timestamp: 1598198400000
    }, {
        step: 3582,
        timestamp: 1598284800000
    }, {
        step: 6979,
        timestamp: 1598371200000
    }, {
        step: 4567,
        timestamp: 1598457600000
    }, {
        step: 7639,
        timestamp: 1598544000000
    }, {
        step: 9150,
        timestamp: 1598630400000
    }, {
        step: 8565,
        timestamp: 1598716800000
    }, {
        step: 6544,
        timestamp: 1598803200000
    }, {
        step: 6316,
        timestamp: 1598889600000
    }, {
        step: 15523,
        timestamp: 1598976000000
    }, {
        step: 8254,
        timestamp: 1599062400000
    }, {
        step: 13552,
        timestamp: 1599148800000
    }, {
        step: 6163,
        timestamp: 1599235200000
    }, {
        step: 6338,
        timestamp: 1599321600000
    }, {
        step: 8508,
        timestamp: 1599408000000
    }, {
        step: 4002,
        timestamp: 1599494400000
    }, {
        step: 7501,
        timestamp: 1599580800000
    }, {
        step: 5922,
        timestamp: 1599667200000
    }, {
        step: 9635,
        timestamp: 1599753600000
    }, {
        step: 7067,
        timestamp: 1599840000000
    }, {
        step: 3878,
        timestamp: 1599926400000
    }, {
        step: 4088,
        timestamp: 1600012800000
    }, {
        step: 773,
        timestamp: 1600099200000
    }]
编写一个函数,将其中某个时间段的苹果数量进行小计,返回小计结果
示例:
输入:2020-09-01, 2020-09-07
返回:64654
怎么做呀,我的思路是遍历元素判断是否符合条件。 取出有用的值放入新数组中用来求和,然后写不出来了,谢谢大佬解答

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

答案应该是58338吧

function getCount(stime, etime){
    stime = new Date(stime)
    etime = new Date(etime)  
    if(etime <= stime){
        alert('结束时间应大于开始时间!')
        return 0
    }
    return steps.reduce((count, item, index) => {
        return count + (item.timestamp < stime || item.timestamp > etime ? 0 : item.step)
    }, 0)
}

getCount('2020-09-01', '2020-09-07') // 58338

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...