javascript - Why calculation code doesnt work? -
so basically, please check 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 + ' @ ' + 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 assign variables, , tried add variables variable array called pricematrix, it's not assigning somehow. think that's because i'm assigning variable it's done in jquery, please try me fix issue?
edit: jsfiddle demo - http://jsfiddle.net/tssvb/2/
when you're grabbing data dom, they're strings not numbers. must force numeric type using parseint()
or parsefloat()
Comments
Post a Comment