Results 1 to 8 of 8
  1. #1

    javascript less than or equal to not working

    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.

  2. #2
    Administrator Dave's Avatar
    Join Date
    Sep 2004
    Location
    Te Awamutu, New Zealand
    Posts
    3,959
    Blog Entries
    79
    Sorry to be a pain but could you provide the URL for your page, or attach the complete web page to your post? It just makes it a lot easier to see what's going on, e.g. to make sure we're seeing all your code and seeing it in the correct context.
    Dave Owen
    MediaCollege.com

  3. #3
    http://www.smoothsellin.com/samplejava.html

    In the box that says Final Auction Bid, type 1000 then hit tab.
    The calculation is correct.

    If you type 100, its still wrong, etc... I have read from other posts that the numbers need to be turned into actual numbers, and they said the best way to do that is to divide by 1, i tried it, but still no luck.

    The reason I didnt post all the code before is because there is a lot of extra code that isnt related to my problem.

    The actual code that need to be fixed is below:

    <textarea cols="60" rows="10">
    &lt;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;

    firsta/1;
    firstp/1;
    seconda/1;
    secondp/1;
    thirda/1;
    thirdp/1;
    lasta/1;
    lastp/1;
    miscfees/1;

    pamount = document.form2.finalsale.value;
    saleamount = pamount/1;

    if(saleamount&lt;=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&lt;=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&lt;=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';
    }

    }

    &lt;/SCRIPT>
    </textarea>

  4. #4
    Administrator Dave's Avatar
    Join Date
    Sep 2004
    Location
    Te Awamutu, New Zealand
    Posts
    3,959
    Blog Entries
    79
    Thanks. I've had a quick look and I can't see anything obvious. Unfortunately I have to go away for about 24 hours -- I will have a closer look when I get back. If you find a solution before then, please let me know.

    I'm afraid there is more bad news regarding your page. If you look at the page in Mozilla it doesn't show the dollar amounts or money order at all.

    PS I put your code in a text box to fix the page layout problem but it inserted all those &lt;br /> tags. Sorry to anyone else trying to make sense of it, I don't have time to fix it right now.
    Dave Owen
    MediaCollege.com

  5. #5
    Dave
    Guest
    I did a quick search and I believe you have found a solution over at webdeveloper.com. If this is not so, let me know and I'll try and help again.

    If it is so, just remember that I did ask you to let me know if you found a solution so I wouldn't be wasting my valuable time doing what someone else has already done for you :wink:

  6. #6
    New Member
    Join Date
    Nov 2008
    Location
    ssd
    Posts
    1
    Hi.....
    The source codes are really useful.....
    Thanks.....

  7. #7
    You seem to have two problems, one of them is related with "string-to-numerical value" conversion.

    so here we go:


    <script type="text/javascript">
    <!--//

    HTML Code:
    var i = '1'; // this is the type of string
    alert(i); // yields 1
    i = i + 2; // strings may be joinned by other strings
    alert(i); // yields 12
    i = parseFloat(i) + 3; // now this isn't a string
    alert(i); // yields 15 :)
    //-->
    </script>


    conclusion: parseFloat your field values, and make sure they're proper numbers.

    The next question is about your logic control. I would have other recommendations but first let me share with you the following javascript knowledge rescourse. Dave you also split this information else where, as I'm sure you'd like the content as much as I do. I found them pretty useful, besides, I think of this site is as much of a reference web site as those that we all liked, such as w3schools, wikipedia.

    best regards;
    kunter.
    Last edited by kunter; 27th Nov 2008 at 15:33. Reason: mis-typo
    - kunter ilalan web designer
    follow him on twitter and on MediaCollege

  8. #8
    parseFloat, nice. i've always multiplied it by one. that woprks too but i'm not sure if it works 100% of the time.

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Subscribe to us on YouTube