Click to See Complete Forum and Search --> : javascript less than or equal to not working


louish
11-22-2004, 02:09 AM
I have NO clue why this isn’t working. My math comparisons aren’t working.

Someone types in a number (saleamount).
I need to find out what price range it falls into:
I have 4 price ranges..
========================
Range 1: 0.00 – 250
Range 2: 250.01 – 500.00
Range 3: 500.01 – 1000.00
Range 4: 1000.01 +
========================
firsta is 250
thirda is 500
lasta is 1000

So.. I am doing this:

if(saleamount<=firsta)
{
document.form2.which.value='Range 1';
}
else if(saleamount<=thirda)
{
document.form2.which.value=' Range 2';
}
else if(saleamount<=lasta)
{
document.form2.which.value=' Range 3';
}
else if(saleamount>=lasta)
{
document.form2.which.value=' Range 4';
}


Now.. I assume that is correct.. if im not correct, then good, how do I fix it. BUT… here are some examples of some numbers I am entering in as the saleamount, and next to it is the range its outputting.

10 = Range 1
100 = Range 1
1000 = Range 1
10000 = Range 1
25 = Range 1
250 = Range 1
26 = Range 2
260 = Range 2
2600 = Range 2
55 = Range 4
550 = Range 4
5500 = Range 4

Its not looking at the number as a whole, its looking at the first digit or something… I don’t get it. That table above should output like this:

10 = Range 1
100 = Range 1
1000 = Range 3
10000 = Range 4
25 = Range 1
250 = Range 1
26 = Range 1
260 = Range 2
2600 = Range 4
55 = Range 4
550 = Range 3
5500 = Range 4

Why are the numbers not being considered actual whole numbers, instead its just looking at the beginning of the number.

Kor
11-22-2004, 02:30 AM
Someone types in a number (saleamount).

Nope. Someoune types in a string. The form's elements' value are always strings, so, before act a math operator upon them you must transform them into numbers

There are a few methods to do that:

saleamount = Number(saleamount)
or
saleamount = saleamount*1
or
saleamount = saleamount/1

or, if you wanna get the integer/floating number
saleamount = ParseInt(saleamount)
saleamount = ParseFloat(saleamount)

7stud
11-22-2004, 03:01 AM
I would list them in the reverse order as to preferability:

1)
parseInt()
parseFloat()

2)
snumber=Number(saleamount);

The Number constructor requires a numeric value. If you call it with a string value, javascript will do an automatic conversion for you, which means javascript might be calling parseInt() or parseFloat() to do the conversion anyway.

3)
snumber = saleamount*1;

Yeech!

Kor
11-22-2004, 04:20 AM
Ok, if you opened the "usability" discussion...

1. Number() and the multiplication/division with/by 1 methods will:

- convert each string into the correspondent integer/floated but only if the characters in strings are digits and dot floating .

Number('50.05') will return 50.05
Number('50.05px') will return NaN

'50.05'*1 will return 50.05
'50.05'/1 will return 50.05

'50.05px'*1 will will return NaN
'50.05px'/1 will will return NaN

Number() has some more other extra facilities, amongst the conversion boolean/binar

Number(false) will return 0
Number(true) will return 1

2. Parse methods are to be used mainly when a string, which is to be denominated as number, is written as measure unit

parseFloat('50.05px') will return 50.05
parseInt('50.05px')will return 50

In fact, parse methods have either extra facilities, such as the possibility to transform into a decimal number from a specified base string written.

parseInt('A',16) will return 10 - as A in hexa is equal with 10 in decimal.

thus the complete parse methods coding is

parseInt(string,base)
parseFloat(string,base)

In conclusion, if the input strings are to be digits and that inputs may be strings of some integer and floaded as well, probably the best way is to use Number() or *1 /1 methods, this is the reason for I started with them. Both assure the floaded conversion, if case.

7stud
11-22-2004, 04:49 AM
Ok, if you opened the "usability" discussion...
Yep. :D

(edited)In conclusion, if the string can be an integer or a float(i.e a number with a decimal point), probably the best way is to use Number(), *1, or /1 methods, this is the reason I started with them. Both assure the string will be converted properly.

1) The poster needs to convert the string to floats(numbers with decimal points) to see if it fits in his range, so parseFloat() seems to do what he wants.

2) You bring up a good point when you point out that the different methods return different results when the string is not all digits. Thanks for straightening me out! Depending on what the script writer wants to happen when the user enters non digits, different conversion methods would be appropriate.

:)

Warren86
11-22-2004, 07:00 AM
<HTML>
<Head>
<Script Language=JavaScript>

var Range1 = 250.00;
var Range2 = 500.00;
var Range3 = 1000.00;
var Range4 = 2000.00;

function rangeCalc(isSale,nRange){

x = false;
if (isSale > eval(nRange)){x = true}
return x;
}

function getRange(isSale){

nRange = 1;
for (i=1; i<5; i++)
{
currRange = "Range"+i
if (rangeCalc(isSale,currRange)){nRange++}
}
Form2.isRange.value = "Range"+nRange;
}

</Script>
</Head>
<Body>
<Form name='Form2'>
Sale Amount: <input type=text size=5 name='saleAmt'><br>
Range: <input type=text size=8 name='isRange'><br><br>
<input type=button value='Show Range' onclick="getRange(saleAmt.value)">
</Form>
</Body>
</HTML>

louish
11-22-2004, 12:20 PM
thank you thank you thank you thank you thank you!!!!!!!!!

Warren86
11-22-2004, 12:22 PM
You're welcome. Good luck with your project.

louish
11-22-2004, 12:27 PM
Well sorry, one more quick thing.. I didnt know your online right now.


Can you actually help me convert my current script into the working method. I tried copying yours, but i think yours just returning which range it is, (Range plus a number), Im actually running a math command depending on the range.

here is my code so far.. can you help me get it to work.


<SCRIPT language=JavaScript>

function calculatecommission()
{

firsta = document.test.firsta.value;
firstp = document.test.firstp.value;
seconda = document.test.seconda.value;
secondp = document.test.secondp.value;
thirda= document.test.thirda.value;
thirdp = document.test.thirdp.value;
lasta = document.test.lasta.value;
lastp = document.test.lastp.value;
miscfees = document.test.miscfees.value;


saleamount = document.form2.finalsale.value;
if(saleamount<=firsta)
{
document.form2.weget.value=Math.round(eval(saleamount*firstp)*100)/100;document.form2.youget.value=Math.round(eval(saleamount-document.form2.weget.value)*100)/100;
document.form2.which.value='1';
}
else if(saleamount<=thirda)
{
document.form2.weget.value=Math.round(eval((saleamount-firsta)*secondp+(firsta*firstp))*100)/100;document.form2.youget.value=Math.round(eval(saleamount-document.form2.weget.value)*100)/100;
document.form2.which.value='2';
}
else if(saleamount<=lasta)
{
document.form2.weget.value=Math.round(eval((saleamount-thirda)*thirdp+((seconda*secondp)+(firsta*firstp)))*100)/100;document.form2.youget.value=Math.round(eval(saleamount-document.form2.weget.value)*100)/100;
document.form2.which.value='3';
}
else (saleamount>lasta)
{
document.form2.weget.value=Math.round(eval((saleamount-lasta)*lastp+((thirda*thirdp)+(seconda*secondp)+(firsta*firstp)))*100)/100;document.form2.youget.value=Math.round(eval(saleamount-document.form2.weget.value)*100)/100;
document.form2.which.value='4';
}
document.test.percent0.value=Math.round((document.form2.weget.value/saleamount)*100)+'%';
document.test.percent1.value=Math.round((document.form2.weget.value/saleamount)*100)-miscfees+'%';
document.test.percent2.value=Math.round((document.form2.youget.value/saleamount)*100)+'%';
document.test.percent3.value=miscfees+'%';
document.test.miscget.value=document.form2.weget.value*.09;
document.test.wegetactual.value=document.form2.weget.value-document.test.miscget.value;
document.test.totalpercent.value='100%';
document.home.checkwrittento.value=document.form2.checkwrittento.value;
document.home.itemtitle.value=document.form2.itemtitle.value;
TestIt()


}

</SCRIPT>

Warren86
11-22-2004, 01:00 PM
Your request isn't "one more quick thing." Forgive me, but I grow weary of the "one more quick thing" routine. You posted code, you asked for a solution to return the actual WORD "Range" and a number. I provided that solution. And you were effusive with your gratitude. You obviously understand the way my code works. But, now, "one more quick thing," isn't.

The code you NOW post is, and the desired result are completely different. Forgive, but you will have to find someone else to try and manipulate.

I do suggest that in the future you state the problem, posting actual code, and not, if you'll forgive the expression, "jack me around." Because, that's what you have done, and I take offense.

I spent time, in good faith, trying to help you, help you with a problem that doesn't exist.

That won't happen again.

louish
11-22-2004, 02:18 PM
Excuse me, but I did post the original code, but just removed the math part:

Code from first post:
if(saleamount<=firsta)
{
document.form2.which.value='Range 1';
}
else if(saleamount<=thirda)
{
document.form2.which.value=' Range 2';
}
else if(saleamount<=lasta)
{
document.form2.which.value=' Range 3';
}
else if(saleamount>=lasta)
{
document.form2.which.value=' Range 4';
}


Actual Code:

if(saleamount<=firsta)
{
document.form2.weget.value=Math.round(eval(saleamount*firstp)*100)/100;document.form2.youget.value=Math.round(eval(saleamount-document.form2.weget.value)*100)/100;
document.form2.which.value='1';
}
else if(saleamount<=thirda)
{
document.form2.weget.value=Math.round(eval((saleamount-firsta)*secondp+(firsta*firstp))*100)/100;document.form2.youget.value=Math.round(eval(saleamount-document.form2.weget.value)*100)/100;
document.form2.which.value='2';
}
else if(saleamount<=lasta)
{
document.form2.weget.value=Math.round(eval((saleamount- thirda)*thirdp+((seconda*secondp)+(firsta*firstp))
)*100)/100;document.form2.youget.value=Math.round(eval(saleamount-document.form2.weget.value)*100)/100;
document.form2.which.value='3';
}
else (saleamount>lasta)
{
document.form2.weget.value=Math.round(eval((saleamount- lasta)*lastp+((thirda*thirdp)+(seconda*secondp)+(f
irsta*firstp)))*100)/100;document.form2.youget.value=Math.round(eval(saleamount-document.form2.weget.value)*100)/100;
document.form2.which.value='4';
}



I just removed the math part, the code is the same. Im sorry you got all mad, I can just take the script you did and modify it to work on my script. You did it so fast, i thought you might be able to impliment it into my actual code, vs me trying to do it would take a long time.

Sorry for asking.

steelersfan88
11-22-2004, 03:59 PM
Kor and I have had this conversation before ... and Kor is right. But to add, the parseInt() fucntion takes a second argument, the base. You should always declare this as 10. Numbers such 08 will be interpreted as base 16 (i think) and you'll get screwed up.

Dr. Script

Kor
11-23-2004, 02:41 AM
eval(nRange)


eval() seems an ambiguous method. When possible I avoid it, it looks full of traps to me :D . It is used mainly to evaluate a function rather than a variable. I keep stubbornly in recomanding the Number() parsin' method...

7stud
11-23-2004, 05:18 AM
But to add, the parseInt() fucntion takes a second argument, the base. You should always declare this as 10. Numbers such 08 will be interpreted as base 16 (i think) and you'll get screwed up.


Yep, I'm aware of that thanks.

vs. me trying to do it would take a long time.

Heaven forbid you might learn something.