0

i have this code where it computes for the time entered by the user.
it doesn't have yet the limitations or the conditions regarding the time to be entered .

it successfully computes for the time and the hour increases based on the minutes, but the problem is when you entered "9" for the hour it doesn't add to the total but the rest is fine.. anyone can help ?

so here it is:

<html>
<FORM>
<table name="tblSamp" border="1">
<tr><th>Time</th>
<td><input type="text" name="alpha" value="" onchange="addColon(this.form.alpha, this.form.total);" maxlength="5"/></td>
<td><input type="text" name="beta" value="" onchange="addColon(this.form.beta, this.form.total);" maxlength="5" /></td>
<td><input type="text" name="gamma" value="" onchange="addColon(this.form.gamma, this.form.total);"  maxlength="5"/></td>
<td><input type="text" name="total" value="" readonly="readonly" /></td>
</tr>

</table>
<input type="hidden" name="displaycount" size="20">
</FORM>

<SCRIPT TYPE="text/javascript">
        var hours = new Array(); 
        var mins = new Array();

    function addColon(what, outTo){

        var tHours = 0;
        var tMins = 0;
        var tTime = 0; 
        var adder = 0;
        var min = 0;
        var tempMins = 0;

    var string = what.value;
    var strlen = string.length;
    var i = string.indexOf(":"); 
    var tbl = document.getElementById("tblSamp");
    var cIndex = what.parentNode.cellIndex; 
    //alert("cell index=" + cIndex); 
    //alert("index of (:): " + i);
    //alert("string length " + strlen);
    if(string != "" & !isNaN(string)){
        if(i < 0){//if colon == 0
            if(strlen < 3){//for input 12 = 12:00
                //alert("for input 12 = 12:00");
                hours[cIndex] = string;
                mins[cIndex] = '00';
                string = string + ':00';
                what.value = string;
            }
            else if(strlen < 4){//for input 123 = 01:23
                //alert("for input 123 = 01:23");
                hours[cIndex] = '0' + string.charAt(0);
                mins[cIndex] = string.charAt(1) + string.charAt(2);
                string = hours[cIndex] + ':' + mins[cIndex];
                //alert(string);
                what.value = string;
            }
            else if(strlen < 5){//for input 1234 = 12:34
                //alert("for input 1234 = 12:34");
                hours[cIndex] = string.charAt(0) + string.charAt(1);
                mins[cIndex] = string.charAt(2) + string.charAt(3);
                string = hours[cIndex] + ':' + mins[cIndex];
                //alert(string);
                what.value = string;
            }
            else if(strlen > 4){//for input 12345 = Invalid Input
                alert("Invalid Input!");
                what.value = "";
            }
        }       
        else {
            alert("Invalid Input!");
            what.value = "";
        }
    }
    else if(string != ""){
        if(i > 0 && i < 3){
            //alert("with :");
            if(strlen < 5){//for input 1:23 = 01:23
                //alert("for input 1:23 = 01:23");
                hours[cIndex] = '0' + string.charAt(0);
                mins[cIndex] = string.charAt(2) + string.charAt(3);
                string = hours[cIndex] + ':' + mins[cIndex];
                //alert(string);
                what.value = string;
            } 
            else {
                //alert("correct input");
                hours[cIndex] = string.charAt(0) + string.charAt(1);
                mins[cIndex] = string.charAt(3) + string.charAt(4);
            }
        }
    }
    //end if
        var lastCol = 4;
        var table = document.getElementById('tblSamp');

            for(var i=1;i<lastCol;i++){
                if(mins[i] != "" && !isNaN(mins[i]) && hours[i] != "" && !isNaN(hours[i])){
                    tMins = eval(tMins) + parseInt(mins[i]);
                    //alert("total mins: "+tMins);
                    if(tMins > 59){
                        tempMins = tMins;
                        min = tempMins % 60;
                        tMins = tMins - min;
                        adder = tMins/60;
                        tHours += adder;
                        tMins = min
                    }
                    tHours = eval(tHours) + parseInt(hours[i]);
                    //alert("total hours: "+tHours);
                }
            }
                tTime = tHours + ':' + tMins;
                //alert("TOTAL=" + tTime);
                outTo.value = tTime;
}
</SCRIPT>

</html>

just copy the whole thing and you can try it ...
i really need help in this. i can't see where did that problem come from ..
cheers,

3
Contributors
6
Replies
7
Views
5 Years
Discussion Span
Last Post by pyTony
0

the problem is not only on 9 numeric.Problem is also if you give any number such as "9:08".Check if you are doing correct calculation.Will look into your logic once my work is done.

0

i'm pretty sure the calculation is correct.. or maybe i'm just missing on something ...
i'm sorry i reaaly need help in this one .. tnx

1

9 does not exist in octal numbers, which has digits in range of 0..7. Octal numbers is signaled with 0 at beginning of number, you can however tell that number is actually base 10 by using optional second parameter to parseInt

<html>
<FORM>
<table name="tblSamp" border="1">
<tr><th>Time</th>
<td><input type="text" name="alpha" value="" onchange="addColon(this.form.alpha, this.form.total);" maxlength="5"/></td>
<td><input type="text" name="beta" value="" onchange="addColon(this.form.beta, this.form.total);" maxlength="5" /></td>
<td><input type="text" name="gamma" value="" onchange="addColon(this.form.gamma, this.form.total);"  maxlength="5"/></td>
<td><input type="text" name="total" value="" readonly="readonly" /></td>
</tr>

</table>
<input type="hidden" name="displaycount" size="20">
</FORM>

<SCRIPT TYPE="text/javascript">
        var hours = new Array();
        var mins = new Array();

    function addColon(what, outTo){

        var tHours = 0;
        var tMins = 0;
        var tTime = 0;
        var adder = 0;
        var min = 0;
        var tempMins = 0;

    var string = what.value;
    var strlen = string.length;
    var i = string.indexOf(":");
    var tbl = document.getElementById("tblSamp");
    var cIndex = what.parentNode.cellIndex;
    //alert("cell index=" + cIndex);
    //alert("index of (:): " + i);
    //alert("string length " + strlen);
    if(string != "" & !isNaN(string)){
        if(i < 0){//if colon == 0
            if(strlen < 3){//for input 12 = 12:00
                //alert("for input 12 = 12:00");
                hours[cIndex] = string;
                mins[cIndex] = '00';
                string = string + ':00';
                what.value = string;
            }
            else if(strlen < 4){//for input 123 = 01:23
                //alert("for input 123 = 01:23");
                hours[cIndex] = '0' + string.charAt(0);
                mins[cIndex] = string.charAt(1) + string.charAt(2);
                string = hours[cIndex] + ':' + mins[cIndex];
                //alert(string);
                what.value = string;
            }
            else if(strlen < 5){//for input 1234 = 12:34
                //alert("for input 1234 = 12:34");
                hours[cIndex] = string.charAt(0) + string.charAt(1);
                mins[cIndex] = string.charAt(2) + string.charAt(3);
                string = hours[cIndex] + ':' + mins[cIndex];
                //alert(string);
                what.value = string;
            }
            else if(strlen > 4){//for input 12345 = Invalid Input
                alert("Invalid Input!");
                what.value = "";
            }
        }
        else {
            alert("Invalid Input!");
            what.value = "";
        }
    }
    else if(string != ""){
        if(i > 0 && i < 3){
            if(strlen < 5){//for input 1:23 = 01:23
                //alert("for input 1:23 = 01:23");
                hours[cIndex] = '0' + string.charAt(0);
                mins[cIndex] = string.charAt(2) + string.charAt(3);
                string = hours[cIndex] + ':';
                if(mins[cIndex].length==1) string +=  '0'
                string += mins[cIndex];

                //alert(string);
                what.value = string;
            }
            else {
                //alert("correct input");
                hours[cIndex] = string.charAt(0) + string.charAt(1);
                mins[cIndex] = string.charAt(3) + string.charAt(4);
            }
        }
    }
    //end if
        var lastCol = 4;
        var table = document.getElementById('tblSamp');

            for(var i=1;i<lastCol;i++){
                if(mins[i] != "" && !isNaN(mins[i]) && hours[i] != "" && !isNaN(hours[i])){
                    //alert("\nhours[i]: "+hours[i]+"->"+parseInt(hours[i], 10)+" mins[i]: "+mins[i]);
                    if(parseInt(mins[i], 10) > 59) {
                       alert("Minutes out of range: " + mins[i])
                       continue;
                    }
                    tMins = tMins + parseInt(mins[i], 10);
                    tHours = tHours + parseInt(hours[i], 10);
                    if(tMins > 59){
                        tHours += 1;
                        tMins = tMins - 60;
                    }
                    //alert("total hours: "+tHours+" total mins: "+tMins);
                }
            }
                tTime = tHours + ':'
                if(tMins < 10) tTime +=  '0'
                tTime += tMins;
                outTo.value = tTime;
}
</SCRIPT>

</html>

Edited by pyTony

0

i've solved this actually .. and yes you're right, it's because of the octal thing.

This question has already been answered. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.