so basically, please check this code –
Full code –
<script type="text/javascript">
$(document).ready(function() {
var seasonLookup = [
{startDay: 1, startMonth:1, endDay: 10, endMonth: 6, season:1},
{startDay: 21, startMonth:9, endDay: 31, endMonth: 12, season:1},
{startDay: 11, startMonth:6, endDay: 30, endMonth: 6, season:2},
{startDay: 1, startMonth:9, endDay: 20, endMonth: 9, season:2},
{startDay: 1, startMonth:7, endDay: 31, endMonth: 8, season:3},
];
var cars = $('#cars_input').val();
var priceone = ($('#cars_input option[value="'+cars+'"]').attr('priceone'));
var pricetwo = ($('#cars_input option[value="'+cars+'"]').attr('pricetwo'));
var pricethree = ($('#cars_input option[value="'+cars+'"]').attr('pricethree'));
var pricefour = ($('#cars_input option[value="'+cars+'"]').attr('pricefour'));
var pricefive = ($('#cars_input option[value="'+cars+'"]').attr('pricefive'));
var pricesix = ($('#cars_input option[value="'+cars+'"]').attr('pricesix'));
var priceseven = ($('#cars_input option[value="'+cars+'"]').attr('priceseven'));
var priceeight = ($('#cars_input option[value="'+cars+'"]').attr('priceeight'));
var pricenine = ($('#cars_input option[value="'+cars+'"]').attr('pricenine'));
var priceMatrix = {
cars: {
1: { t1: priceone, t2: pricetwo, t3: pricethree},
2: { t1: pricefour, t2: pricefive, t3: pricesix},
3: { t1: priceseven, t2: priceeight, t3: pricenine}
}
};
function getSeason(date){
var day = date.getDate();
var month = date.getMonth()+1;
var year = date.getFullYear();
for(var i=0;i<seasonLookup.length;i++){
var s = seasonLookup[i];
var startDate = new Date(year, s.startMonth-1,s.startDay);
var endDate = new Date(year, s.endMonth-1,s.endDay);
if(date >= startDate && date <= endDate)
return s.season;
}
return null;
}
function getPrice(bike, season, days){
var tier = "";
if(days <=2)
tier = "t1";
else if(days <=7)
tier = "t2";
else
tier = "t3"
console.log(days + ' days in season ' + season + ' at ' + priceMatrix[bike][season][tier] + '/day (' + tier + ')')
return priceMatrix[bike][season][tier] * days;
}
function calculatePrice(startDate, endDate, bike)
{
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;
}
$('.recalc').change(function(){
var startDate = new Date(parseInt($('#year_input').val(),10),parseInt($('#month_input').val(),10)-1,parseInt($('#day_input').val(),10) );
var endDate = new Date(parseInt($('#yearr_input').val(),10),parseInt($('#monthr_input').val(),10)-1,parseInt($('#dayr_input').val(),10));
var bike = $('#bike').val();
var price = calculatePrice(startDate,endDate,bike);
$('#car-price').val(price);
});
$('#cars_input').change();
});
</script>
What I’ve done there is assign a variables, and then I tried to add the variables to variable array called priceMatrix, but it’s not assigning somehow. I think that’s because I’m assigning variable as it’s done in jquery, could you please try help me fix the issue?
EDIT: JSFIDDLE Demo – http://jsfiddle.net/tSsvb/2/
When you’re grabbing the data from the DOM, they’re strings not numbers. You must force the numeric type by using
parseInt()orparseFloat()