Basically, I need to fix this script, I tried to fix it, but it returns -1 of days.
You can see the new script here –
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;
}
}
which returns -1 and this is the script which returns everything just fine –
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;
}
}
Why I need to edit the script? Simply, because it returns days in current season, but I need to inclue in totalPrice total days and days in current season. Or another possibility is that I need to include the tier in getprice, as shown above, both should work.
Hope to hear help :)!
if “now” and “startDate” are objects, then every time you call
now.setDate(something)
you also change startDate… because now and startDate are pointers that point to the same object.
I’m not sure if that’s causing your error because I don’t see where you use startDate… but it could be changing it in the caller, for example.
edit: yes that is your problem, currentDate, now, and startDate all point to the same date object — so your second while loop never executes.