zoukankan      html  css  js  c++  java
  • JavaScript实战:计算产品销售额

    实战

    (1)案例描述:随机生成某产品一年中每天的销售额,并计算某一月日平均销售额以及某一周日平均销售额。

    (2)实现思路:

    1)假设该产品日销售额区间为(0,10000),运用Math对象的random()方法随机生成区间内的随机数;

    2)创建三维数组month用于存放每个月每一周每一天的日销售额数据。

    <html>
        <head>
            <title>计算产品销售额</title>
        </head>
        <script>
            var WeekSales = function()
            {
                this.month=[];
                this.init = init;
                this.getAverageSomeMonth = getAverageSomeMonth;
                this.getAverageSomeWeek = getAverageSomeWeek;
                
            };
            var init=function(month)
            {
                var i=0;j=0;k=0;
                for(i=0;i<month;i++)
                {            
                    this.month[i] = [];
                    for(k=0;k<4;k++)
                    {
                        this.month[i][k]=[];
                        for(j=0;j<7;j++)
                        {
                            this.month[i][k][j]=parseInt(Math.random()*10000);
                            
                        }
                    }
                }
            };
            function getAverageSomeMonth(month)
            {
                month = month || 12;
                console.log(month+"月的日平均销售额:"+this.month[month-1].map(function(arr)
                {return arr.reduce(function (a,b){return a+b;});            
                }).reduce(function(a,b){return a+b;})/28)
            }
            function getAverageSomeWeek(month,week)
            {
                month = month || 12;
                week = week||1;
                console.log(month+"月第"+week+"周的日平均销售额:"+
                this.month[month-1][week-1].reduce(function(a,b){return a+b;})/7);
            }
            var newWeekSales = new WeekSales();
            newWeekSales.init(12);
            newWeekSales.getAverageSomeMonth(12);
            newWeekSales.getAverageSomeMonth(12,4);
        </script>
    </html>

    运行结果如下(随机的):

     

  • 相关阅读:
    22(1).模型融合---Random Forest
    Tesseract 模块
    非线性问题的三种处理方法
    jupyter 快捷键
    回归评价指标---MSE、RMSE、MAE、R-Squared
    理解JavaScript中的事件处理
    jquery事件重复绑定解决办法
    IE6 IE7 IE8(Q) 不支持 JSON 对象
    浏览器事件机制与自定义事件的实现
    浏览器加载和渲染html的顺序
  • 原文地址:https://www.cnblogs.com/daitu/p/12771272.html
Copyright © 2011-2022 走看看