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 Complex Javascript Array - sum values and insert value into form
 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.
01-02-2013, 08:04 PM   PM User | #1
jmill23
New to the CF scene

Join Date: Jan 2013
Posts: 6
Thanks: 0
Thanked 0 Times in 0 Posts
Complex Javascript Array - sum values and insert value into form

Trying to adapt a rather complex array code. Basically, I want to add the total hours entered, then return the value into input name="total_hrs", but got stuck! Would also appreciate any help to simplify/clean up the script! Thanks. Here's the script:
Quote:

And the html:
Quote:


Activity

Time
(in hours)

Calories used per hour

Sleeping

hrs

kcals

Watching TV

hrs

kcals

Sitting

hrs

kcals

Cooking

hrs

kcals

Standing

hrs

kcals

Washing & Dressing

hrs

kcals

Walking Slowly

hrs

kcals

Light housework

hrs

kcals

Walking moderately

hrs

kcals

Light gardening

hrs

kcals

Dancing

hrs

kcals

Walking up stairs

hrs

kcals

Jogging

hrs

kcals

Squash

hrs

kcals

Totals =

hrs

kcals

 01-02-2013, 08:36 PM PM User | #2 Old Pedant Supreme Master coder!     Join Date: Feb 2009 Posts: 24,949 Thanks: 75 Thanked 4,307 Times in 4,274 Posts You need to combine your totalCalc and subCalc functions into a single function. You need subCalc to return TWO DIFFERENT VALUES, and it can't (easily) do that. Separating them into two functions serves no purpose. Also, kill your roundFloat() and checkAmount() functions. They aren't doing anything useful that can't be done simpler. __________________ 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.
 01-02-2013, 09:10 PM PM User | #3 Old Pedant Supreme Master coder!     Join Date: Feb 2009 Posts: 24,949 Thanks: 75 Thanked 4,307 Times in 4,274 Posts Here's how I would do it. Note that by making your field names more readable you make it easier to give a readable error message when bad values are input. Code: ```
Enter your times spent at each activity IN MINUTES:
Sleeping:
TV:
sitting:
cooking:
standing:
washing:
walking slowly:
housework:
walking medium:
gardening:
dancing:
stairs:
jogging:
playing squash:

Total hours:
Total calories:

``` __________________ 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.
 01-02-2013, 09:27 PM PM User | #4 jmill23 New to the CF scene   Join Date: Jan 2013 Posts: 6 Thanks: 0 Thanked 0 Times in 0 Posts Thanks Old Pedant, code looks a lot simpler now, but after trying it's not calculating correctly. Each variable is based on time, activity and a set value. The kcals bit was working ok, but isn't now. The hours is now working in your code but isn't adding up correctly e.g. 1 + 2 hrs is giving a result of 0.05?
01-02-2013, 09:33 PM   PM User | #5
jmill23
New to the CF scene

Join Date: Jan 2013
Posts: 6
Thanks: 0
Thanked 0 Times in 0 Posts
Quote:
 Originally Posted by Old Pedant Also, kill your roundFloat() and checkAmount() functions. They aren't doing anything useful that can't be done simpler.
Just removed roundFloat() and checkAmount() functions and it doesn't work as these are linked to by:
// Adds total hours.
hours = totalHrs
form.total_hrs.value = checkAmount(hours);

 01-02-2013, 10:38 PM PM User | #6 Old Pedant Supreme Master coder!     Join Date: Feb 2009 Posts: 24,949 Thanks: 75 Thanked 4,307 Times in 4,274 Posts You didn't read a VERY IMPORTANT PART of my page. Code: `Enter your times spent at each activity IN MINUTES:
` If you really want to input in hours instead (who wants to enter 0.25 hours instead of just 15 minutes???) just remove the line that says Code: ` time /= 60; // convert to hours` __________________ 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.
01-02-2013, 10:39 PM   PM User | #7
Old Pedant
Supreme Master coder!

Join Date: Feb 2009
Posts: 24,949
Thanks: 75
Thanked 4,307 Times in 4,274 Posts
Quote:
 Originally Posted by jmill23 Just removed roundFloat() and checkAmount() functions and it doesn't work as these are linked to by: // Adds total hours. hours = totalHrs form.total_hrs.value = checkAmount(hours);
** SIGH **

Look at my ALL NEW code.

Where, for example, I do
Code:
```    form.total_hrs.value = totalTime.toFixed(2);
form.total.value = totalCalories.toFixed(0);```
(in place of your way-overkill checkAmount() code).
__________________
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.

01-03-2013, 10:16 AM   PM User | #8
jmill23
New to the CF scene

Join Date: Jan 2013
Posts: 6
Thanks: 0
Thanked 0 Times in 0 Posts
Quote:
 Originally Posted by Old Pedant You didn't read a VERY IMPORTANT PART of my page. Code: `Enter your times spent at each activity IN MINUTES:
` If you really want to input in hours instead (who wants to enter 0.25 hours instead of just 15 minutes???) just remove the line that says Code: ` time /= 60; // convert to hours`
Hi,
Thought about that, but the calorie ratios are based on hours of activities.

Thanks.

01-03-2013, 10:18 AM   PM User | #9
jmill23
New to the CF scene

Join Date: Jan 2013
Posts: 6
Thanks: 0
Thanked 0 Times in 0 Posts
Quote:
 Originally Posted by Old Pedant ** SIGH ** Look at my ALL NEW code. Where, for example, I do Code: ``` form.total_hrs.value = totalTime.toFixed(2); form.total.value = totalCalories.toFixed(0);``` (in place of your way-overkill checkAmount() code).
Thanks - I know the script is a bit extensive! That's why I got stuck - I'm not an expert - just trying to adapt it! Could you simplify it so it is shorter as there are scrpits passing through scripts....

01-03-2013, 09:11 PM   PM User | #10
Old Pedant
Supreme Master coder!

Join Date: Feb 2009
Posts: 24,949
Thanks: 75
Thanked 4,307 Times in 4,274 Posts
Quote:
 Originally Posted by jmill23 Hi, Thought about that, but the calorie ratios are based on hours of activities. Thanks.
Yes. I know. And that's why, *BEFORE* I multiply the time by the number of calories per hour, I *FIRST DIVIDE BY 60* to convert the minutes into hours.

But as I said, if you want to stick with hours just remove the ONE LINE that I indicated (and of course change the message).
__________________
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.

01-03-2013, 09:34 PM   PM User | #11
Old Pedant
Supreme Master coder!

Join Date: Feb 2009
Posts: 24,949
Thanks: 75
Thanked 4,307 Times in 4,274 Posts
Quote:
 Originally Posted by jmill23 Could you simplify it so it is shorter as there are scrpits passing through scripts....
Ummm...I *THOUGHT* that was what I did in my post #3.

Did you actually *TRY* that code???

The HTML isn't as pretty as yours, but it works. I really did test it, you know.

Oh...what the heck. Here. I've add the ability to choose to use hours or minutes. And I've put it online to prove it works.
http://mywhizbang.com/calories.html

You can VIEW-->>SOURCE that page to see the updated code.
__________________
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.

 01-04-2013, 09:41 AM PM User | #12 jmill23 New to the CF scene   Join Date: Jan 2013 Posts: 6 Thanks: 0 Thanked 0 Times in 0 Posts Thanks Old Pedant! It looks good and I did try it before!

 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 07:04 PM.