基本上,我需要修复此脚本,我试图修复它,但它返回-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;
}
}
为什么我需要编辑脚本?简单地说,因为它返回当前季节的天数,但我需要包括当前季节的总天数和天数。或者另一种可能性是我需要在getprice中包含层,如上所示,两者都应该有效。
希望听到帮助:))
答案 0 :(得分:0)
如果“now”和“startDate”是对象,那么每次调用
now.setDate(东西)
你也改变了startDate ...因为now和startDate是指向同一个对象的指针。
我不确定这是否会导致您的错误,因为我没有看到您在哪里使用startDate ...但它可能会在调用者中更改它,例如。
编辑:是你的问题,currentDate,now和startDate都指向同一个日期对象 - 所以你的第二个while循环永远不会执行。