还剩1页未读,继续阅读
文本内容:
实训案例名称计算产品销售额案例描述
1.随机生成某产品一年中每天的销售额,并计算某一个月日平均销售额以及某一周日平均销售额实现思路
2.假设该产品日销售额区间为,首先运用对象的方法随机生成区间内的随机数,10,10000Math random代码片段如下var sales=parselntMath.random*10000;创建三维数组用于存放每个月每一周每一天的日销售额数据如表示第个月2month month[i]i+1的销售额数据;皿表示第个月中第周的销售额数据;皿表示第个月第周第month[i i+1j+1month[i]k]i+1j+1k+1天的销售额数据;并用随机生成的销售额填充这个三维数组,代码片段如下var init=function month{var i=0,j=0,k=0;for i=0;imonth;i++{口;this.month[i]=for k=0;k4;k++{this.month[i][k]=0;forj=0;j7;j++{this.month[i][k][j]=parselntMath.random*10000;}创建函数用来计算某个月的日平均销售额,传入参数为整数月份运用数组的3getAverageSomeMonth方法返回该月中每周的日销售额之和,并用数组的叩方法接收结果,然后再运用方法将该月每reduce mreduce周销售额相加得到该月的总销售额,最后将结果除以即得到该月日平均销售额代码片段如下28function getAverageSomeMonthmonth{month=month||12;月的日平均销售额是:“+console.Iogmonth+”this.month[month-1].mapfunctionarr{returnarr.reducefunctiona,b{return a+b;};}.reducefunctiona,b{return a+b;}/28;创建函数用来计算某一周的日平均销售额,传入参数为整数月份和整数周4getAverageSomeWeek运用数组的方法返回该月中当前计算周的日销售额之和,最后将结果除以即得到该月该周日平均reduce7销售额代码片段如下function getAverageSomeWeekmonth,week{month=month||12;week=week||1;”月第”+”周的日平均销售额是:+console.logmonth+week+this.month[month-1][week-1].reducefunctiona,b{return a+b;}/7;最后创建对象数组并调用函数传入参数即可5实现代码
3.完整代码如脚本所示5-3脚本5-
3.html!DOCTYPE htmlhtmlheadmetacharset=utf-8,,某产品的日平均销售额计算title v/tiQe/headbodyscript type=,,text/javascript,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;imonth;i++{this.month[i]=[];for k=0;k4;k++{this.month[i][k]=[];forj=0;j7;j++{this.month[i][k][j]=parselntMath.random*10000;月周://console.logi+1+“”+k+1++this.month[i][k].toString;function getAverageSomeMonthmonth{month=month||12;月的日平均销售额是:“+console.Iogmonth+”this.month[month-1].mapfunctionarr{return arr.reducefunctiona,b{return a+b;};}.reducefunctiona,b{retum a+b;}/28;function getAverageSomeWeekmonth,week{month=month||12;week=week||1;”月第+”周的日平均销售额是:“+console.Iogmonth+week+this.month[month-1][week-1].reducefunctiona,b{return a+b;}/7;var newWeekSales=new WeekSales;newWeekSales.init12;newWeekSales.getAverageSomeMonth12;newWeekSales.getAverageSomeWeek12,4;/script/body/html运行结果如图所示
5.5图案例运行结果
5.55-1。
个人认证
优秀文档
获得点赞 0