• 02-17-2013, 08:16 PM
samyouel
I'm pretty new to javascript - having only started it a couple of week ago... So here's my question.

I'm basically using functions and if else if statements to build an electricity reading calculator.

The units given is 1236 which is a parameter of the function called elecReading. This will be used as the amount of units used and it will calculate the amount that must be paid.

However, 0-500 units are billed at \$1 per unit. 500 - 1000 units are billed at \$1.10 a unit, and over 1000 units are billed at \$3.20 a unit. I'm unsure how I can get this working without breaking down 1236 into singular numbers manually. Is their a piece of javascript code that could do this?

Obviously i'm not asking for the complete answer, but a push in the right direction would be very helpful at this stage!

Thanks for the help in advance,
• 02-17-2013, 08:35 PM
I'm not quite sure what you are asking since you said you are already using if/else statements. Isn't this what you want?

Code:

```var units = 1236; // this would be your parameter if( units < 0 ) {     // error } else if( units <= 500 ) {     var perUnitPrice = 1.00; } else if( units <= 1000 ) {     var perUnitPrice = 1.10; } else {     var perUnitPrice = 3.20; }```
By the way, please note that "0–500 is \$1/unit and 500–1000 is \$1.10/unit" is misleading (are 500 units \$1/unit or \$1.10/unit?), the above implementation assumes you meant 501–1000 is \$1.10/unit.
• 02-18-2013, 08:55 AM
Philip M
My understanding is that the first 500 units are charged at \$1 per unit, the next 500 units are charged at \$1.1 per unit, and units over 1000 are charged at \$3.20 a unit. The reverse of the usual tariff system where a higher numbers of units attract a lower unit price!

Code:

```<script type = "text/javascript"> var units = 1236; if( units < 0 ) {     // error } var over1000 = units - 1000; if (over1000 <0) {over1000 = 0} var rem = units - over1000; var over500 = rem - 500; if (over500 <0) {over500 = 0} rem = rem - over500; var price = (over1000 * 3.2) + (over500 * 1.1) + (rem * 1); alert (price.toFixed(2)); // 1805.20 </script>```
Be aware that it is not really in your best interests that others do your all or most homework for you. Many people would regard that as cheating. Furthermore your teacher may gain a false and exaggerated idea of your programming capabilities and so not offer you the support you need. Also, if you hand in other people's work which you do not completely understand, then you will start to fall behind and your difficulties will increase. Finally, there is a limit to the number of times that you can take your pitcher to this particular well!

The secret of success in life is honesty and fair dealing. If you can fake that, you've got it made. - Groucho Marx (1890 - 1977)
• 02-18-2013, 08:55 PM
Old Pedant
Quote:

Originally Posted by Philip M
The reverse of the usual tariff system where a higher numbers of units attract a lower unit price!

Not true, here in the USA, for utilities that are considered in short supply.

For example:
Electricity, measured in kilowatt hours, costs more if you use too much.
Water, measured in cubic feet, costs more if you use too much.

Can't think of other examples, off hand, though.
• 02-18-2013, 09:08 PM
Philip M
Quote:

Originally Posted by Old Pedant
Not true, here in the USA, for utilities that are considered in short supply.

For example:
Electricity, measured in kilowatt hours, costs more if you use too much.
Water, measured in cubic feet, costs more if you use too much.

Can't think of other examples, off hand, though.

You tell me something! :) In The UK tariffs tend to reduce as quantities consumed increase.
• 02-18-2013, 09:17 PM
Old Pedant

1 can (UK "bin", yes?) of garbage per week might cost \$5.
2 cans would cost maybe \$12.
3 cans perhaps \$25.

You are encouraged to recycle instead of creating non-recyclable garbage. We actually get a (very small) rebate on our trash collection from putting out recyclable materials separately.
• 02-18-2013, 09:28 PM
Philip M
Quote:

Originally Posted by Old Pedant