2012-04-18 59 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が指すポインタですので、「今」と「たstartDateは」あなたはもたstartDateを変更

now.setDate(何か)

を呼び出すたびに...そして、オブジェクトである場合同じオブジェクト。

startDateをどこで使用しているのかわからないため、エラーが発生するかどうかはわかりませんが、たとえば発信者側で変更する可能性があります。

編集:はいあなたの問題は、currentDate、now、startDateがすべて同じ日付オブジェクトを指しているので、2度目のwhileループは決して実行されません。

+0

私はそれを修正してもらえますか? –

+0

ああそれを固定!ありがとうm8!あなたは最高です! –