![]() |
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:
Quote:
|
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. |
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:
<html> |
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?
|
Quote:
// Adds total hours. hours = totalHrs form.total_hrs.value = checkAmount(hours); |
You didn't read a VERY IMPORTANT PART of my page.
Code:
Enter your times spent at each activity IN MINUTES:<hr/>Code:
time /= 60; // convert to hours |
Quote:
Look at my ALL NEW code. Where, for example, I do Code:
form.total_hrs.value = totalTime.toFixed(2); |
Quote:
Thought about that, but the calorie ratios are based on hours of activities. Thanks. |
Quote:
|
Quote:
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). |
Quote:
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. |
Thanks Old Pedant! It looks good and I did try it before!
|
| All times are GMT +1. The time now is 04:28 AM. |
Powered by vBulletin®
Copyright ©2000 - 2013, Jelsoft Enterprises Ltd.