javascript计算不计算
基本上,我需要修复这个脚本,我试图修复它,但它返回-1天。 您可以在这里看到新脚本-javascript计算不计算,javascript,jquery,Javascript,Jquery,基本上,我需要修复这个脚本,我试图修复它,但它返回-1天。 您可以在这里看到新脚本- function calculatePrice(startDate, endDate, bike) { if(cars != "no") { console.log(startDate); console.log(endDate); var currentSeason = getSeason(startDate);
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;
}
}
函数计算价格(开始日期、结束日期、自行车){
如果(汽车!=“否”){
控制台日志(起始日期);
console.log(endDate);
var currentSeason=getSeason(起始日期);
var totalPrice=0;
var daysInSeason=0;
var currentDate=起始日期;
var tierss=“”;
var now=起始日期;
var-daye=0;
而(now如果“now”和“startDate”是对象,那么每次调用
现在。设置日期(某物)
您还可以更改startDate…因为now和startDate是指向同一对象的指针
我不确定这是否导致了您的错误,因为我不知道您在哪里使用startDate…但它可能会在调用方中更改它,例如
编辑:是的,这就是你的问题,currentDate、now和startDate都指向同一个日期对象——因此你的第二个while循环永远不会执行。你能帮我解决这个问题吗?
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;
}
}