Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 7 of 7
  1. #1
    New to the CF scene
    Join Date
    Feb 2013
    Posts
    3
    Thanks
    0
    Thanked 0 Times in 0 Posts

    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,

  • #2
    Regular Coder
    Join Date
    Jan 2013
    Location
    Germany
    Posts
    578
    Thanks
    4
    Thanked 77 Times in 77 Posts
    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.

  • #3
    Supreme Master coder! Philip M's Avatar
    Join Date
    Jun 2002
    Location
    London, England
    Posts
    18,310
    Thanks
    203
    Thanked 2,563 Times in 2,541 Posts
    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)
    Last edited by Philip M; 02-18-2013 at 01:57 PM. Reason: Typo

    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.

  • #4
    Supreme Master coder! Old Pedant's Avatar
    Join Date
    Feb 2009
    Posts
    27,664
    Thanks
    80
    Thanked 4,641 Times in 4,603 Posts
    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.

  • #5
    Supreme Master coder! Philip M's Avatar
    Join Date
    Jun 2002
    Location
    London, England
    Posts
    18,310
    Thanks
    203
    Thanked 2,563 Times in 2,541 Posts
    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.

  • #6
    Supreme Master coder! Old Pedant's Avatar
    Join Date
    Feb 2009
    Posts
    27,664
    Thanks
    80
    Thanked 4,641 Times in 4,603 Posts
    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.

  • #7
    Supreme Master coder! Philip M's Avatar
    Join Date
    Jun 2002
    Location
    London, England
    Posts
    18,310
    Thanks
    203
    Thanked 2,563 Times in 2,541 Posts
    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.


  •  

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •