How Can I Do This Arithmetic In JS
May 1, 2006
If I wanted to do this simple arithmetic in JS can it be achieved please?
var engineSizeSlider = document.getElementById("engine_size_max").innerHTML + 1;
document.getElementById("engine_size_max").innerHTML is equal to say 5, and what is happening is 1 is being applied to make 15 rather than 6. Is there any way to force simple arithmetic rules?
View 2 Replies
Nov 21, 2005
<html>
<head>
<script type="text/javascript">
var fullDayName = new Array("Sunday","Monday","Tuesday","Wednesday","Thursday","Friday","Saturday","Sunday")
function verify(isField){
var splitDate = isField.value.split("/");
var refDate = new Date(isField.value);
if (splitDate[0] < 1 || splitDate[0] > 12 || refDate.getDate() != splitDate[1] || splitDate[2].length != 4 || (!/^19|20/.test(splitDate[2]))){return false}
return refDate;
}
function calcDay(isForm){
var startDate = verify(isForm.nStart);
var n = 0;
if (startDate)
{
var offset = isForm.nOffset.value;
if (offset > 0)
{
for (i=1; n<offset; i++)
{
var tmp = new Date(startDate.getFullYear(),startDate.getMonth(),startDate.getDate()+i);
if (tmp.getDay() != 0 && tmp.getDay() != 6){n++}
}
i--;
}
else {
for (i=1; n>offset; i++)
{
var tmp = new Date(startDate.getFullYear(),startDate.getMonth(),startDate.getDate()-i);
if (tmp.getDay() != 0 && tmp.getDay() != 6){n--}
}
i = (i*-1)+1;
}
var resultDate = new Date(startDate.getFullYear(),startDate.getMonth(),startDate.getDate()+i);
var slashDate = resultDate.getMonth()+1+"/"+resultDate.getDate()+"/"+resultDate.getFullYear();
isForm.nResult.value = slashDate;
isForm.dayName.value = fullDayName[resultDate.getDay()];
}
if (!startDate)
{
alert('Invalid Date')
isForm.nStart.value = "";
isForm.nStart.focus();
}
}
</script>
</head>
<body>
<br>
<form name='Form1'>
<Table align='center' cellspacing=Ɔ' cellpadding=Ƌ' style='font-size:14pt;border:solid black 1px;background-color:lightyellow;'>
<THead><TH colspan=ƈ' style='background-color:lightblue;border-bottom:solid black 1px'>Weekday Arithmetic</TH></THead>
<TFoot><TR><TD colspan=ƈ' align='center' style='border-top:solid black 1px;Font-Size:11pt;background-color:moccasin'>Input format = mm/dd/yyyy</TD></TR></TFoot>
<TBody>
<TR><TD align='left'>Reference Date:</TD><TD align='right'><input type='text' size=ཆ' name='nStart' onclick="this.value=''this.form.nResult.value=''this.form.dayName.value=''"></TD></TR>
<TR><TD align='left'>Weekdays + or - : </TD><TD align='right'><input type='text' size=ཆ' name='nOffset' onclick="this.value=''this.form.nResult.value=''this.form.dayName.value=''"></TD></TR>
<TR><TD align='left'>Result Date: </TD><TD align='right'><input type='text' size=ཆ' readonly name='nResult'></TD></TR>
<TR><TD align='left'>Day of Week: </TD><TD align='right'><input type='text' size=ཆ' readonly name='dayName' style='text-align:center'>
<TR><TD colspan=ƈ' align='center' style='border-top:solid black 1px;background-color:darkorange'><input type='button' value='Calculate' onclick="calcDay(this.form)"></TD></TR>
</TBody>
</Table>
</form>
</body>
</html>
View 1 Replies
View Related