2012-04-18 101 views
0

基本上,我需要修复这个脚本,我试图修复它,但它返回-1天。 你可以在这里看到新的脚本 -javascript计算不计算

function calculatePrice(startDate, endDate, bike) {       
    if(cars != "no") { 
     console.log(startDate); 
     console.log(endDate); 
     var currentSeason = getSeason(startDate); 
     var totalPrice = 0; 
     var daysInSeason = 0; 
     var currentDate = startDate;   
     var tierss = "";  
     var now = startDate;   
     var daye = 0; 
     while(now <= endDate) { 
     var season = getSeason(currentDate); 
     daye++; 
     now.setDate(now.getDate() + 1);  
     } 
     if(daye <= 3) tierss = "t1"; 
     else if (daye <= 8) tierss = "t2"; 
     else tierss = "t3"          
     while (currentDate <= endDate) {      
      var season = getSeason(currentDate); 
      if (season != currentSeason) { 
       totalPrice += getPrice(bike, currentSeason, daysInSeason, tierss); 
       currentSeason = season; 
       daysInSeason = 0; 
      } 
      daysInSeason++; 
      console.log('days in season - ' + daysInSeason); 
      currentDate.setDate(currentDate.getDate() + 1); 
     }             
     totalPrice += getPrice(bike, currentSeason, daysInSeason, tierss); 
     return totalPrice; 
    } 
    else { 
     totalPrice = 0; 
     return totalPrice; 
    } 
} 

返回-1,这是返回一切就好了剧本 -

function calculatePrice(startDate, endDate, bike) {       
    if(cars != "no") { 
    console.log(startDate); 
    console.log(endDate); 
    var currentSeason = getSeason(startDate); 
    var totalPrice = 0; 
    var daysInSeason = 0; 
    var currentDate = startDate; 
    while (currentDate <= endDate) { 
     var season = getSeason(currentDate); 
     if (season != currentSeason) { 
      totalPrice += getPrice(bike, currentSeason, daysInSeason); 
      currentSeason = season; 
      daysInSeason = 0; 
     } 
     daysInSeason++; 
     currentDate.setDate(currentDate.getDate() + 1); 
    } 
    totalPrice += getPrice(bike, currentSeason, daysInSeason); 
    return totalPrice; 
    } 
    else { 
    totalPrice = 0; 
    return totalPrice; 
    } 
} 

为什么我需要编辑脚本?简而言之,因为它会在当前季节返回日期,但我需要总计包含当前季节中的总天数和天数。或者另一种可能性是我需要在getprice中包含该层,如上所示,两者都应该有效。

希望听到帮助:)!

回答

0

如果“现在”和“的startDate”的对象,那么你每次打电话

now.setDate(东西)

还改变的startDate ...因为现在且StartDate是指向指针同一个对象。

我不确定这是否导致你的错误,因为我没有看到你在哪里使用startDate ...但它可以改变它在调用者,例如。

编辑:是的,这是你的问题,currentDate,现在,和startDate都指向相同的日期对象 - 所以你的第二个while循环永远不会执行。

+0

你能帮我解决吗? – 2012-04-18 16:30:37

+0

哦修好了!感谢m8!你是最好的! – 2012-04-18 16:39:02