Sunday, 15 April 2012

javascript - Electricity units calculator -



javascript - Electricity units calculator -

i'm using functions , if else if statements build electricity reading calculator.

the units given 1236 parameter of function called elecreading. used amount of units used , calculate amount must paid.

however, first 0-500 units billed @ $1 per unit. next 500-1000 units billed @ $1.10 unit, , on 1000 units billed @ $3.20 unit. example, if used 1000 units, bill $1050.

i'm unsure how can working without breaking downwards 1236 singular numbers manually. how can write calculator javascript?

obviously i'm not asking finish answer, force in right direction helpful @ stage!

thanks help in advance

the static version like:

var unit_price_1001_over = 3.20; var unit_price_501_1000 = 1.10; var unit_price_under_500 = 1.00; function elecreading(units) { var cost = 0; if (units > 1000) { cost += (units-1000) * unit_price_1001_over; units = 1000; } if (units > 500) { cost += (units - 500) * unit_price_501_1000; units = 500; } cost += units * unit_price_under_500; homecoming price; }

this assuming unit cost ranges 1-500, 501-1000, 1001-inf. can done more / less hardcoding, using list of objects representing cost range + cost per unit in said range.

javascript function parameters units-of-measurement

No comments:

Post a Comment