根据所选计划查找总金额



我有一个7天的产品计划,产品价格不同,

如果用户选择了周一,周二,周三的计划,则其各自的价格为100,200,300
现在我必须向用户提供这个产品7次。

用户将在周一,周二,

周三获得产品,然后在下周再次获得周一,周二,周三,该周之后仅在周一

所以我同样需要计算它的总价格

var total = 100 * 3 , 200 * 2 , 300 * 2 ; 

如何实现这一点?到目前为止我已经尝试过,

var mon=document.getElementById("mon").value;
var tue=document.getElementById("tue").value;
var wed=document.getElementById("wed").value;
var thu=document.getElementById("thu").value;
var fri=document.getElementById("fri").value;
var sat=document.getElementById("sat").value;
var sun=document.getElementById("sun").value;
var total=0;
for(i=1;i<=7;i++){
if(mon != ''){ 
total=total+ +mon;
}
if(tue != ''){
total=total+ +tue;
}
if(wed != ''){
total=total+ +wed;
}
if(thu != ''){
total=total+ +thu;
}
if(fri != ''){
total=total+ +fri;
}
if(sat != ''){
total=total+ +sat;
}
if(sun != ''){
total=total+ +sun;
}
alert(total );
}

但这是不正确的.周一数7次

// Put  values into array
a = [];
var a[0]=document.getElementById("mon").value;
var a[1]=document.getElementById("tue").value;
var a[3]=document.getElementById("wed").value;
...
// Dont forget to test that sum(a) != 0 to avoid endless loop 
var total=0;
i = 7;
p = 0;
// Loop while all 7 days will be found
while(i>0) {
  // Find next not empty
  while(a[p] == '') p = ++p % 7;
  total += parseInt(a[p]);
  i--;
  p = ++p % 7
}
console.log(total );

演示

最新更新