代码之家  ›  专栏  ›  技术社区  ›  Javascript Coder

创建JavaScript函数,根据一些信息计算价格(示例-商店报价)

  •  0
  • Javascript Coder  · 技术社区  · 7 年前

    我有一个数组 [2,5,1] 美元,即价格,客户支付 2 第一件商品为$,因为第一件商品不打折, 5-2 = 3 min(1st item, 2nd item) min(2,5) = 2

    so 2+3+0 is output of calculate price.
    

    如果我还有一个输入

    [2,5,1,6]
    2 + (5-2) + 0 + (6 - min(2,5,1))
    

    我怎样才能做到这一点-

    我试着写这样的东西-

    function calculateAmount(prices) {
        // Write your code here
        var totalCostPurchase;
        var cost = [];
        var zero = 0;
        for (var i = 0; i < prices.length; i++) {
            if (i === 0) {
                cost.push(prices[i]);    
            } else if (i === 1) {
                cost.push(prices[i] - cost[i - 1]);
            } else {
                var minCost = Math.min(...prices.slice(0,i));
                console.log(minCost);
                if (minCost > prices[i]) {
                    cost.push(zero);
                } else {
                    cost.push(prices[i] - minCost);
                }
            } 
        }
        totalCostPurchase = cost.reduce((a, b) => a + b, 0);
        console.log(totalCostPurchase);
        return totalCostPurchase;
    }
    
    Please guide.
    
    3 回复  |  直到 7 年前
        1
  •  1
  •   Nitish Narang    7 年前

    你可以用 Array.map 和 Array.reduce 为此

    var a = [2,5,1,6]
    
    let res = a.map((d,i) => i != 0 
                  ? d - Math.min(...a.slice(0, i+1))
                  : d)
               .reduce((x, y) => x + y)
                 
    
    console.log(res)
        2
  •  1
  •   slider    7 年前

    reduce

    function total(prices) {
      return prices.reduce(([sum, min], p, i) => {
        if (i === 0) return [p, p];
        let newSum = sum;
        if (p >= min) newSum += p - min;
        return [newSum, Math.min(p, min)];
      }, [])[0];
    }
    
    
    console.log(total([2, 5, 1]));
    console.log(total([2, 5, 1, 6]));
        3
  •  0
  •   Javascript Coder    7 年前

    我尝试了答案,这似乎适用于所有情况,这可能不是最好的解决方案,但这就是我所想的。

    function calculateAmount(prices) {
        // Write your code here
        var totalCostPurchase;
        var cost = [];
        var zero = 0;
        for (var i = 0; i < prices.length; i++) {
            if (i === 0) {
                cost.push(prices[i]); 
            } else if (i === 1) {
                if ((prices[i] - cost[i - 1]) < 0) { 
                    cost.push(zero);
                } else {
                    cost.push(prices[i] - cost[i - 1]);
                }
            } else {
                var minCost = Math.min(...prices.slice(0,i));
                if (minCost > prices[i]) {
                    cost.push(zero);
                } else {
                    if ((prices[i] - minCost) < 0) {
                        cost.push(zero);
                    } else {
                        cost.push(prices[i] - minCost);
                    }
                }
            } 
        }
        totalCostPurchase = cost.reduce((a, b) => a + b, 0);
        return totalCostPurchase;
    }