Flash Website Builder- Trendy Site Builder is a Flash Site Building tool that helps users build stunning websites. Check Out Custom Custom Logo Design by LogoBee. Website Design and Free Logo Templates available.
 CodingForums.com Electric Reading Calculator
 User Name Remember Me? Password

Before you post, read our: Rules & Posting Guidelines

Enjoy an ad free experience by logging in. Not a member yet? Register.
 02-17-2013, 09: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 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,
 02-17-2013, 09:35 PM PM User | #2 Airblader 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.
 02-18-2013, 09:55 AM PM User | #3 Philip M Supreme Master coder!     Join Date: Jun 2002 Location: London, England Posts: 17,493 Thanks: 200 Thanked 2,471 Times in 2,449 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: `````` 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 01:57 PM.. Reason: Typo
02-18-2013, 09:55 PM   PM User | #4
Old Pedant
Supreme Master coder!

Join Date: Feb 2009
Posts: 24,965
Thanks: 75
Thanked 4,309 Times in 4,276 Posts
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.
__________________
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.

02-18-2013, 10:08 PM   PM User | #5
Philip M
Supreme Master coder!

Join Date: Jun 2002
Location: London, England
Posts: 17,493
Thanks: 200
Thanked 2,471 Times in 2,449 Posts
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.
__________________

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.

 02-18-2013, 10:17 PM PM User | #6 Old Pedant Supreme Master coder!     Join Date: Feb 2009 Posts: 24,965 Thanks: 75 Thanked 4,309 Times in 4,276 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.
02-18-2013, 10:28 PM   PM User | #7
Philip M
Supreme Master coder!

Join Date: Jun 2002
Location: London, England
Posts: 17,493
Thanks: 200
Thanked 2,471 Times in 2,449 Posts
Quote:
 Originally Posted by Old Pedant 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.

 Bookmarks

 Thread Tools Rate This Thread Rate This Thread: 5 : Excellent 4 : Good 3 : Average 2 : Bad 1 : Terrible

 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 Rules
 Forum Jump User Control Panel Private Messages Subscriptions Who's Online Search Forums Forums Home :: Client side development     JavaScript programming         DOM and JSON scripting         Ajax and Design         JavaScript frameworks         Post a JavaScript     HTML & CSS     XML     Flash & ActionScript         Adobe Flex     Graphics and Multimedia discussions     General web building         Site reviews         Building for mobile devices :: Server side development     Apache configuration     Perl/ CGI     PHP         Post a PHP snippet     MySQL         Other Databases     Ruby & Ruby On Rails     ASP     ASP.NET     Java and JSP     Other server side languages/ issues         ColdFusion         Python :: Computing & Sciences     Computer Programming     Computer/PC discussions     Geek News and Humour Web Projects and Services Marketplace     Web Projects         Small projects (quick fixes and changes)         Medium projects (new script, new features, etc)         Large Projects (new web application, complex features etc)         Unknown sized projects (request quote)         Vacant job positions         Looking for work/ for hire         Project collaboration/ partnership         Paid work offers and requests (Now CLOSED)     Career, job, and business ideas or advice     Domains, Sites, and Designs for sale         Domains for sale         Websites for sale         Design templates and graphics for sale :: Other forums     Member Offers     Forum feedback and announcements

All times are GMT +1. The time now is 05:04 PM.