Go Back   CodingForums.com > :: Client side development > JavaScript programming

Before you post, read our: Rules & Posting Guidelines

Reply
 
Thread Tools Rate Thread
Enjoy an ad free experience by logging in. Not a member yet? Register.
Old 02-17-2013, 08:16 PM   PM User | #1
samyouel
New to the CF scene

 
Join Date: Feb 2013
Posts: 3
Thanks: 0
Thanked 0 Times in 0 Posts
samyouel is an unknown quantity at this point
Electric Reading Calculator

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,
samyouel is offline   Reply With Quote
Old 02-17-2013, 08:35 PM   PM User | #2
Airblader
Regular Coder

 
Join Date: Jan 2013
Location: Germany
Posts: 348
Thanks: 3
Thanked 43 Times in 43 Posts
Airblader can only hope to improve
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.
Airblader is offline   Reply With Quote
Old 02-18-2013, 08:55 AM   PM User | #3
Philip M
Supreme Master coder!

 
Philip M's Avatar
 
Join Date: Jun 2002
Location: London, England
Posts: 17,033
Thanks: 197
Thanked 2,410 Times in 2,388 Posts
Philip M has a spectacular aura aboutPhilip M has a spectacular aura aboutPhilip M has a spectacular aura about
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)
__________________

All the code given in this post has been tested and is intended to address the question asked.
Unless stated otherwise it is not just a demonstration.

Last edited by Philip M; 02-18-2013 at 12:57 PM.. Reason: Typo
Philip M is offline   Reply With Quote
Old 02-18-2013, 08:55 PM   PM User | #4
Old Pedant
Supreme Master coder!

 
Old Pedant's Avatar
 
Join Date: Feb 2009
Posts: 23,172
Thanks: 59
Thanked 3,993 Times in 3,962 Posts
Old Pedant is a name known to allOld Pedant is a name known to allOld Pedant is a name known to allOld Pedant is a name known to allOld Pedant is a name known to allOld Pedant is a name known to all
Quote:
Originally Posted by Philip M View Post
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.
__________________
An optimist sees the glass as half full.
A pessimist sees the glass as half empty.
A realist drinks it no matter how much there is.
Old Pedant is offline   Reply With Quote
Old 02-18-2013, 09:08 PM   PM User | #5
Philip M
Supreme Master coder!

 
Philip M's Avatar
 
Join Date: Jun 2002
Location: London, England
Posts: 17,033
Thanks: 197
Thanked 2,410 Times in 2,388 Posts
Philip M has a spectacular aura aboutPhilip M has a spectacular aura aboutPhilip M has a spectacular aura about
Quote:
Originally Posted by Old Pedant View Post
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.
__________________

All the code given in this post has been tested and is intended to address the question asked.
Unless stated otherwise it is not just a demonstration.
Philip M is offline   Reply With Quote
Old 02-18-2013, 09:17 PM   PM User | #6
Old Pedant
Supreme Master coder!

 
Old Pedant's Avatar
 
Join Date: Feb 2009
Posts: 23,172
Thanks: 59
Thanked 3,993 Times in 3,962 Posts
Old Pedant is a name known to allOld Pedant is a name known to allOld Pedant is a name known to allOld Pedant is a name known to allOld Pedant is a name known to allOld Pedant is a name known to all
Whoops...forgot about garbage/trash collection.

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.
__________________
An optimist sees the glass as half full.
A pessimist sees the glass as half empty.
A realist drinks it no matter how much there is.
Old Pedant is offline   Reply With Quote
Old 02-18-2013, 09:28 PM   PM User | #7
Philip M
Supreme Master coder!

 
Philip M's Avatar
 
Join Date: Jun 2002
Location: London, England
Posts: 17,033
Thanks: 197
Thanked 2,410 Times in 2,388 Posts
Philip M has a spectacular aura aboutPhilip M has a spectacular aura aboutPhilip M has a spectacular aura about
Quote:
Originally Posted by Old Pedant View Post
Whoops...forgot about garbage/trash collection.

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.

For your passing interest, garbage collection in the UK is included in the Council Tax (property tax). We have two large plastic bins, recyclable (blue) and non-recyclable (black), which are emptied on alterate weeks. Some councils (local authorities) impose a fine on those who put stuff in the wrong bin! Garden waste is placed in a green bin, and collected separately - here you do pay an extra charge to hire the green bin. The trouble with this is that most of our garden waste is generated in the autumn - fall in America - (tree leaves), at which time the bins are not nearly big enough, while for much of the year the bins are close to empty. In our case a compost heap suffices for much of the year.
__________________

All the code given in this post has been tested and is intended to address the question asked.
Unless stated otherwise it is not just a demonstration.
Philip M is offline   Reply With Quote
Reply

Bookmarks

Jump To Top of Thread


Thread Tools
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump


All times are GMT +1. The time now is 06:05 AM.


Advertisement
Log in to turn off these ads.