0

基本上,我需要修复这个脚本,我试图修复它,但它返回 -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;         
      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;
  }
}

为什么我需要编辑脚本?简单地说,因为它返回当前季节的天数,但我需要在 totalPrice 中包含当前季节的总天数和天数。或者另一种可能性是我需要在 getprice 中包含该层,如上所示,两者都应该工作。

希望听到帮助:)!

4

1 回答 1

0

如果“now”和“startDate”是对象,那么每次调用

now.setDate(某事)

您还更改了 startDate... 因为 now 和 startDate 是指向同一个对象的指针。

我不确定这是否会导致您的错误,因为我看不到您在哪里使用 startDate ...但它可能会在调用者中更改它,例如。

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

于 2012-04-18T16:25:52.727 回答