Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 5 of 5
  1. #1
    New to the CF scene
    Join Date
    Aug 2009
    Posts
    4
    Thanks
    0
    Thanked 0 Times in 0 Posts

    calculation problem that's made me bald!

    Here's the script part:

    var price0 = (rate * length).toFixed(0);
    var cut = Number(document.form.cut.value);

    if (cut >=1 <=3) {
    var cuts = cut*100;}
    if (cut >=4 <=10) {
    var cuts = cut*80;}
    if (cut >10) {
    var cuts = cut*60;}

    document.form.price1.value = price0 + cuts;

    If I remove + cuts the price shows accurately.

    With + cuts added, the price1 = price0 multiplied by 100 plus 100.

    Any help greatly appreciated.

  • #2
    Supreme Master coder! Philip M's Avatar
    Join Date
    Jun 2002
    Location
    London, England
    Posts
    17,919
    Thanks
    203
    Thanked 2,531 Times in 2,509 Posts
    toFixed() converts the value to a string. So document.form.price1.value = price0 + cuts; concatenates the two values, not adds them.

    You need to change your code to

    var price0 = parseInt((rate * length),10);
    and then
    document.form.price1.value = (price0 + cuts).toFixed(0);

    but if your display is integer numbers there is not need to convert price0 to an integer using parseInt();

    if (cut >=1 <=3) {
    should be if ((cut>=1) || (cut <=3)) {

    var cut = Number(document.form.cut.value); - avoid using the same name for an HTML element and a Javascript variable.


    "Dives sum, si non redo eis quibus debeo. - I am a rich man as long as I do not pay my creditors"
    Last edited by Philip M; 07-19-2012 at 02:35 PM.

    All the code given in this post has been tested and is intended to address the question asked.
    Unless stated otherwise it is not just a demonstration.

  • #3
    New to the CF scene
    Join Date
    Aug 2009
    Posts
    4
    Thanks
    0
    Thanked 0 Times in 0 Posts
    hmmm... I must have got something wrong...

    var cuts = Number(document.form.cut.value);

    if ((cuts>=1) || (cuts <=3)) {
    var cutstotal = (cuts*100);
    }
    if ((cuts >=4) || (cuts<=10)) {
    var cutstotal = (cuts*80);
    }
    if (cuts >10) {
    var cutstotal = (cuts*60);
    }

    var price0 = parseInt((rate * length),10);
    document.form.price1.value = (price0 + cutstotal).toFixed(0);

    *************
    with two cuts, the price1 value should be price0 + 2 X 100, it comes out at 159.

  • #4
    Supreme Master coder! Philip M's Avatar
    Join Date
    Jun 2002
    Location
    London, England
    Posts
    17,919
    Thanks
    203
    Thanked 2,531 Times in 2,509 Posts
    Error - your || or should be && and.

    Code:
    if ((cuts>=1) && (cuts <=3)) {  // more than or equal to 1, less than or equal to 3
    var cutstotal = (cuts*100);
    alert (cutstotal)
    }
    if ((cuts >=4) && (cuts<=10)) {   // more than or equal to 4, less than or equal to 10
    var cutstotal = (cuts*80);
    }
    Make sure that cuts cannot be a decimal number say 3.5. Use parseInt().
    Last edited by Philip M; 07-19-2012 at 03:17 PM.

    All the code given in this post has been tested and is intended to address the question asked.
    Unless stated otherwise it is not just a demonstration.

  • #5
    New to the CF scene
    Join Date
    Aug 2009
    Posts
    4
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Awesome

    Even saw a hair growing...


  •  

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •