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 2 of 2
  1. #1
    Regular Coder
    Join Date
    Aug 2003
    Location
    scotland, edinburgh
    Posts
    176
    Thanks
    5
    Thanked 1 Time in 1 Post

    add times to give total time

    I posted before about the same code but this time the problem is differnt and therefore I made a new thread for it. So heres the problem.

    I currently have this code that displays the total time when you enter in 3 times.

    PHP Code:
    <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN" "http://www.w3.org/TR/html4/strict.dtd"
    <
    html lang="en"
        <
    head
            <
    title>48897</title
        </
    head
        <
    body
            <
    form onsubmit="makeTotal();return false;"
                <
    table
                    <
    thead
                        <
    tr
                            <
    th>Minutes</th
                            <
    th>Seconds</th
                        </
    tr
                    </
    thead
                    <
    tbody
                        <
    tr
                            <
    td><input type="text" onblur="makeTotal()"></td
                            <
    td><input type="text" onblur="makeTotal()"></td
                        </
    tr
                        <
    tr
                            <
    td><input type="text" onblur="makeTotal()"></td
                            <
    td><input type="text" onblur="makeTotal()"></td
                        </
    tr
                        <
    tr
                            <
    td><input type="text" onblur="makeTotal()"></td
                            <
    td><input type="text" onblur="makeTotal()"></td
                        </
    tr
                    </
    tbody
                </
    table
                 
            </
    form

            <
    p id="result">Default Value</p

             

            <
    script type="text/javascript"
                var 
    result document.getElementById("result"); 
                
    result.style.display "none"

                function 
    makeTotal() 
                { 
                    var list = 
    document.getElementsByTagName("form")[0].getElementsByTagName("table")[0].getElementsByTagName("input"); 
                    var 
    total 0

                    for(var 
    0< list.lengthi++) 
                    { 
                        
    total += (2) ? Number(list[i].value) : Number(list[i].value) * 60
                    } 

                    var 
    time = new Date(total 1000); 

                    
    result.firstChild.nodeValue time.getUTCHours() + ":" time.getUTCMinutes() + ":" time.getUTCSeconds(); 
                    
    result.style.display ""
                } 
            
    </script> 
             
        </body> 
    </html> 
    That worked fine untill i put it to practice where there more than 3 times to be entered ( 68 to be exact ) .
    see it here http://www.eugamers.com/hl2dq/hl2dqupdate.php

    What I noticed was that when the total time appeared all it said was Nan:Nan:Nan
    I was then told thas was becuase it was reading in all the input boxes including the ones which werent related to the time to be calculated. I was then told to change the for loop to this

    PHP Code:
    for(var 1< list.lengthi+=2) {
    total += Number(list[i++].value) + Number(list[i++].value) * 60;

    Although that change cured the NAN error. It displayed the wrong total time. As when I added a second, a minute was added in the total time and when I added a minute, a second was added in the total time.

    Since I have basically no javascipt knowledge, would you be able to show me where and how I would be able to fix the logical error in the new code

    thanks again for your time and knowledge.

  • #2
    Supreme Master coder! glenngv's Avatar
    Join Date
    Jun 2002
    Location
    Philippines
    Posts
    11,068
    Thanks
    0
    Thanked 256 Times in 252 Posts
    Try this:
    Code:
    var result = document.getElementById("result");
    result.style.display = "none";
    function makeTotal()
    {
    	var f = document.forms[0];
    	var total = 0;
    	var fieldMin, fieldSec;
    	var ctr = 1;
    
    	while (typeof (fieldMin = f.elements["m"+ctr]) != "undefined" && typeof (fieldSec = f.elements["s"+ctr]) != "undefined")
    	{
    		total += (Number(fieldMin.value) * 60) + Number(fieldSec.value);
    		ctr++;
    	}
    
    	var time = new Date(total * 1000);
    
    	var mm = time.getUTCMinutes();
    	var ss = time.getUTCSeconds();
    	if (mm < 10) mm = "0" + mm;
    	if (ss < 10) ss = "0" + ss;
    
    	result.firstChild.nodeValue = time.getUTCHours() + ":" + mm + ":" + ss;
    	result.style.display = "";
    }
    Last edited by glenngv; 12-15-2004 at 05:55 AM.
    Glenn
    ____________________________________

    My Blog
    Tower of Hanoi Android app (FREE!)
    Tower of Hanoi Leaderboard
    Samegame Facebook App
    vBulletin Plugins
    ____________________________________


  •  

    Posting Permissions

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