![]() | |
![]() |
| | Thread Tools | Display Modes |
#1
| |||
| |||
|
#2
| ||||
| ||||
|
|
Hi, I am a javascript newbie. Can anyone here help me with the following please ? I am probably making a number of obvious mistakes but I cannot work out what is wrong. :-( { var name1 = parseInt(document.myForm.firstNo.value); |
|
var name2 = parseInt(document.myForm.lastNo.value); var name3 = parseInt(document.myForm.firstNo.value); var name4 = parseInt(document.myForm.lastNo.value); if(name1 < name2) document.myForm.firstNo.value = (name1); document.myForm.lastNo.value = (name2); |
|
else document.myForm.firstNo.value = (name4); document.myForm.lastNo.value = (name3); |
|
} |
#3
| ||||
| ||||
|
|
John Fitzsimons said the following on 3/20/2006 1:20 AM: Hi, I am a javascript newbie. Can anyone here help me with the following please ? I am probably making a number of obvious mistakes but I cannot work out what is wrong. :-( { var name1 = parseInt(document.myForm.firstNo.value); parseInt is known to be inefficient when getting a number from a string. Also, if you are going to use parseInt, be aware of the problem when not providing it the second parameter - the radix (or base) to use. |
|
var name1 = +document.myForm.firstNo.value; var name2 = parseInt(document.myForm.lastNo.value); var name3 = parseInt(document.myForm.firstNo.value); var name4 = parseInt(document.myForm.lastNo.value); if(name1 < name2) document.myForm.firstNo.value = (name1); document.myForm.lastNo.value = (name2); if (name1<name2){ document.myForm.firstNo.value = name1; document.myForm.lastNo.value = name2; } else document.myForm.firstNo.value = (name4); document.myForm.lastNo.value = (name3); else{ document.myForm.firstNo.value = name4; document.myForm.lastNo.value = name3; } Note the curly brackets I have that you don't. It makes a lot of difference. { } } |
|
Although the first if is not needed, the only thing you need is the else part and not sure why you are using 4 variables |
|
when only 2 are needed: function swapThem(){ var number1 = +document.myForm.firstNo.value; var number2 = +document.myForm.lastNo.value; if (number1 < number2) { document.myForm.firstNo.value = number2; document.myForm.lastNo.value = number1; } } Is all that is needed to do what your script does. |
#4
| |||
| |||
|
|
On Mon, 20 Mar 2006 01:57:21 -0500, Randy Webb HikksNotAtHome (AT) aol (DOT) com> wrote: John Fitzsimons said the following on 3/20/2006 1:20 AM: Hi, I am a javascript newbie. Can anyone here help me with the following please ? I am probably making a number of obvious mistakes but I cannot work out what is wrong. :-( { var name1 = parseInt(document.myForm.firstNo.value); parseInt is known to be inefficient when getting a number from a string. Also, if you are going to use parseInt, be aware of the problem when not providing it the second parameter - the radix (or base) to use. Thanks, but I am not clear what "second parameter" you are talking about. Do you mean name2 ? I am also not quite clear on the "base". Are you meaning whole numbers, or decimal, etc. ? The input will be whole (positive) numbers. No decimals. |

|
var name1 = +document.myForm.firstNo.value; var name2 = parseInt(document.myForm.lastNo.value); var name3 = parseInt(document.myForm.firstNo.value); var name4 = parseInt(document.myForm.lastNo.value); if(name1 < name2) document.myForm.firstNo.value = (name1); document.myForm.lastNo.value = (name2); if (name1<name2){ document.myForm.firstNo.value = name1; document.myForm.lastNo.value = name2; } else document.myForm.firstNo.value = (name4); document.myForm.lastNo.value = (name3); else{ document.myForm.firstNo.value = name4; document.myForm.lastNo.value = name3; } Note the curly brackets I have that you don't. It makes a lot of difference. { } } Ahhrrgh ! Thanks. I thought it was something staring me in the face. Although the first if is not needed, the only thing you need is the else part and not sure why you are using 4 variables My thinking was that as soon as var number2 replaced varnumber1 the swap of varnumber1 to varnumber2 position would mean that both positions had the same resulting number. Adding a third and fourth variable placeholder avoids that. |
![]() |
| Thread Tools | |
| Display Modes | |
| |