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
    Nov 2012
    Posts
    2
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Writing a simple array or for loop

    Hi all. Apologies in advance, as I'm a JS noob. I'm working in a print medium that uses JS for variables and my code works. However, it's messy so I've been trying to rewrite it more efficiently using an array and a loop and counter, but to no avail.

    Basically, the user fills out certain fields (like a spreadsheet almost), then I change those values from strings to numbers and total them. It works, it's just I want to learn how to write something much cleaner and more efficient. Thanks in advance!

    Here's the code thus far:

    LineTotal1 = StringToNumber(Field("Linetotal1"));
    LineTotal2 = StringToNumber(Field("Linetotal2"));
    LineTotal3 = StringToNumber(Field("Linetotal3"));
    LineTotal4 = StringToNumber(Field("Linetotal4"));
    LineTotal5 = StringToNumber(Field("Linetotal5"));
    LineTotal6 = StringToNumber(Field("Linetotal6"));
    LineTotal7 = StringToNumber(Field("Linetotal7"));
    LineTotal8 = StringToNumber(Field("Linetotal8"));
    LineTotal9 = StringToNumber(Field("Linetotal9"));
    LineTotal10 = StringToNumber(Field("Linetotal10"));
    Discount = StringToNumber(Field("Discount"));
    Tax = StringToNumber(Field("Tax"));


    var Total = LineTotal1
    + LineTotal2
    + LineTotal3
    + LineTotal4
    + LineTotal5
    + LineTotal6
    + LineTotal7
    + LineTotal8
    + LineTotal9
    + LineTotal10
    - Discount
    + Tax;

    return Total;

  • #2
    Supreme Master coder! Old Pedant's Avatar
    Join Date
    Feb 2009
    Posts
    27,613
    Thanks
    80
    Thanked 4,635 Times in 4,597 Posts
    Why did you invent your own StringToNumber when it's built into JS in several different ways.

    i.e.,
    Code:
    num = Number(str);
    num = parseFloat(str);
    num = 1 * str;
    num = + str;  // not a typo...use a unary plus
    num = parseInt(str); // if you want to be sure you get integer
    There are subtle differences in those, but surely any of them are good enough for most work.

    And why did you invent a Field( ) function when, again, that comes naturally?

    Anyway:
    Code:
    var Total = 0;
    for ( var n = 1; n <= 10; ++n )
    {
        total += StringToNumber(Field("Linetotal" + n) );
    }
    Total -= StringToNumber(Field("Discount"));
    Total += StringToNumber(Field("Tax"));
    But *DO* think about using what is already builtin instead of reinventing wheels.

    Example:
    Code:
    var form = document.forms[0];
    var Total = 0;
    for ( var n = 1; n <= 10; ++n )
    {
        total += Number( form["Linetotal" + n].value );
    }
    Total -= Number(form.Discount.value);
    Total += Number(form.Tax.value);
    An optimist sees the glass as half full.
    A pessimist sees the glass as half empty.
    A realist drinks it no matter how much there is.

  • Users who have thanked Old Pedant for this post:

    nittwitt103 (11-08-2012)

  • #3
    New to the CF scene
    Join Date
    Nov 2012
    Posts
    2
    Thanks
    1
    Thanked 0 Times in 0 Posts
    Hello! Yes, you are 100% correct. Those are already built in, but I'm using a form of VDP software that has it's own unique "building blocks". For instance I tried using "parseInt" to no avail in another area (though I may have been guilty of doing it wrong). It's doubly hard because I'm learning JS and having to learn this as well.

    You help is much appreciated though!

  • #4
    Supreme Master coder! Old Pedant's Avatar
    Join Date
    Feb 2009
    Posts
    27,613
    Thanks
    80
    Thanked 4,635 Times in 4,597 Posts
    I assume you are, for some reason, required to use that VDP stuff. Too bad. It's kind of like being told to write an essay in Spanish while you are studying Italian. Yeah, there are cognates, but the aren't exactly the same.
    An optimist sees the glass as half full.
    A pessimist sees the glass as half empty.
    A realist drinks it no matter how much there is.

  • #5
    Senior Coder rnd me's Avatar
    Join Date
    Jun 2007
    Location
    Urbana
    Posts
    4,461
    Thanks
    11
    Thanked 600 Times in 580 Posts
    Quote Originally Posted by nittwitt103 View Post
    Hello! Yes, you are 100% correct. Those are already built in, but I'm using a form of VDP software that has it's own unique "building blocks". For instance I tried using "parseInt" to no avail in another area (though I may have been guilty of doing it wrong). It's doubly hard because I'm learning JS and having to learn this as well.

    You help is much appreciated though!
    if javascript logic works correctly, there are low-level computer-science substitutions you can use.


    if you don't need it to be an integer strictly speaking but just a number that will add instead of concat, you can use either

    Code:
     val="123";
     (val *1) === Number( val ) // times one
    or
    Code:
     val="123";
     +val === Number( val ) // leading plus
    those are harder to read than Number(), Math.floor(), parseFloat() and ParseInt(), but they add up.


    for integers, you can do other ones to floor or round:

    Code:
    var num=Math.random();
    parseInt(  num * 10  )  ===  (0| num * 10) ;
    aka:
    Code:
    var num=Math.random();
    Math.floor(  num * 10  )  ===  (0| num * 10) ;

    as a callable function:
    Code:
    function int(number){ return 0|number;}
    if your numbers are known to be positive and you want to round it instead of floor it,
    Code:
    val ="123.876";
    +val+.5|0
    if the numbers are negative, you have to flip the sign, round it and unflip...
    Last edited by rnd me; 11-08-2012 at 03:06 PM.
    my site (updated 2014/10/20)
    BROWSER STATS [% share] (2014/9/03) IE7:0.1, IE8:4.3, IE11:9.2, IE9:2.7, IE10:2.6, FF:16.8, CH:47.5, SF:7.8, NON-MOUSE:37%


  •  

    Posting Permissions

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