HighDots Forums  

if/else problem. :-(

JavaScript discussion (multi-lingual) JavaScript discussion (alt.comp.lang.javascript)


Discuss if/else problem. :-( in the JavaScript discussion (multi-lingual) forum.



Reply
 
Thread Tools Display Modes
  #1  
Old   
John Fitzsimons
 
Posts: n/a

Default if/else problem. :-( - 03-20-2006 , 12: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);
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);
}

The two input numbers should result in no change on the page,
or a change if the first number is higher than the second. Any help
appreciated.

Regards, John.

Reply With Quote
  #2  
Old   
Randy Webb
 
Posts: n/a

Default Re: if/else problem. :-( - 03-20-2006 , 12:57 AM






John Fitzsimons said the following on 3/20/2006 1:20 AM:
Quote:
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;

Quote:
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;
}

Quote:
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. { }

Quote:
}
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.
--
Randy
comp.lang.javascript FAQ - http://jibbering.com/faq & newsgroup weekly
Javascript Best Practices - http://www.JavascriptToolbox.com/bestpractices/


Reply With Quote
  #3  
Old   
John Fitzsimons
 
Posts: n/a

Default Re: if/else problem. :-( - 03-20-2006 , 06:21 AM



On Mon, 20 Mar 2006 01:57:21 -0500, Randy Webb
<HikksNotAtHome (AT) aol (DOT) com> wrote:

Quote:
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.

Quote:
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.

Quote:
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.

Quote:
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.
Thanks. Your help is much appreciated. That got things working. I will
also study your alternative approach too. :-)

Regards, John.


Reply With Quote
  #4  
Old   
Randy Webb
 
Posts: n/a

Default Re: if/else problem. :-( - 03-20-2006 , 08:09 AM



John Fitzsimons said the following on 3/20/2006 7:21 AM:
Quote:
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.
parseInt takes two parameters:

parseInt(valToParse,baseToUse);

alert(parseInt('09'));

in your browser and you might be surprised
http://jibbering.com/faq/#FAQ4_12


Quote:
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.
number1 and number2 do not get replaced. The value in the text field
gets replaced but that doesn't change number1 and number2

--
Randy
comp.lang.javascript FAQ - http://jibbering.com/faq & newsgroup weekly
Javascript Best Practices - http://www.JavascriptToolbox.com/bestpractices/


Reply With Quote
Reply




Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off



Powered by vBulletin Version 3.5.4
Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.