javascript arrays 2nd try

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • meltedown

    #1

    javascript arrays 2nd try

    I'm trying to keep track of how many items of each price are sold so if
    4 items of the same price are ordered one is free.

    for example,

    5 items at $17 , and 8 items at $4

    In php, I would just make an array:
    myarray[$dollars]+=$items;
    This would make an array like:
    myarray[17]=5
    myarray[8]=4
    then I would iterate through the array with foreach:
    foreach(myarray as $dollars=>$item s){

    //for every 3 items at this price, add $dollars to $discount
    }
    $total-=$discount;

    I beleive I have made a similar javascript array with
    myarray[unit[i].value]=qty[i].value;

    But how do I iterate through this array ?
  • Jc

    #2
    Re: javascript arrays 2nd try

    meltedown wrote:[color=blue]
    > But how do I iterate through this array ?[/color]

    You may want to brush up on javascript control statements, such as the
    for loop. See the group FAQ for a list of javascript references, here's
    one for JScript (Microsoft's implementation of javascript, click on the
    Statements section to see the for loop documentation):
    http://msdn.microsoft.com/library/de...eReference.asp

    Are you sure you are comfortable calculating prices in client-side
    script? I assume the next step is to post this information back to the
    server... what's going to stop someone from manually posting fake
    pricing to the server? Pricing and discount logic is usually just
    displayed to the user, and calculated on the server, where it isn't as
    easy to tamper with.

    Comment

    • meltedown

      #3
      Re: javascript arrays 2nd try

      Jc wrote:[color=blue]
      > meltedown wrote:
      >[color=green]
      >>But how do I iterate through this array ?[/color]
      >
      >
      > You may want to brush up on javascript control statements, such as the
      > for loop. See the group FAQ for a list of javascript references, here's
      > one for JScript (Microsoft's implementation of javascript, click on the
      > Statements section to see the for loop documentation):
      > http://msdn.microsoft.com/library/de...eReference.asp
      >[/color]
      I don't understand that website.

      I know how to use a for loop, but I don't see what good a for loop is
      for iterating through an associative array. how do I access the key and
      the value ?[color=blue]
      > Are you sure you are comfortable calculating prices in client-side
      > script? I assume the next step is to post this information back to the
      > server... what's going to stop someone from manually posting fake
      > pricing to the server? Pricing and discount logic is usually just
      > displayed to the user, and calculated on the server, where it isn't as
      > easy to tamper with.[/color]

      I don't see what the problem is. The user can change the number of
      items, but not the price.[color=blue]
      >[/color]

      Comment

      • Jc

        #4
        Re: javascript arrays 2nd try

        meltedown wrote:[color=blue]
        > I know how to use a for loop, but I don't see what good a for loop is
        > for iterating through an associative array. how do I access the key and
        > the value ?[/color]

        This should be answered in your first post on this topic.
        [color=blue][color=green]
        > > Are you sure you are comfortable calculating prices in client-side
        > > script? I assume the next step is to post this information back to the
        > > server... what's going to stop someone from manually posting fake
        > > pricing to the server? Pricing and discount logic is usually just
        > > displayed to the user, and calculated on the server, where it isn't as
        > > easy to tamper with.[/color]
        >
        > I don't see what the problem is. The user can change the number of
        > items, but not the price.[/color]

        You are thinking in terms of what your webpage allows the user to do.
        Even though the price is not exposed through an editable control on the
        webpage, like the quantity is, that does not mean it is tamper-proof.

        If the price is being calculated client-side (on the user's computer),
        then that is within the user's power to control, and it must have to be
        transmitted to the server at some point, assuming there is some sort of
        online system involving invoice generation, email notifications, and
        perhaps even online credit card transactions.

        It would be less of a concern if your page is only used by the client
        to print out an order to be mailed in, but I am assuming that there is
        some sort of online transaction occurring based on this
        javascript-generated price.

        If this javascript-generated price gets sent back to the server, it is
        fairly trivial for a user to bypass your webpage and communicate
        directly with the server, sending it an order for which they get
        charged a price of their choosing. This could be done as simply as
        typing the order and price information as parameters on the URL.

        Comment

        • VK

          #5
          Re: javascript arrays 2nd try

          > I beleive I have made a similar javascript array

          What is that new trend to mix array and hash in one term? No one calls
          integer as float or class as object. But array and hash - like no
          problem... Is it some new stuff from Computer Science university
          department?

          If you are doing *array", then you have to use only positive integer
          values for array index, as it was since ALGOL.

          Hash (Associative array) doesn't exists in JavaScript as a separate
          programming entity. But each object inherits internal hash mechanics
          from Object() constructor. In hash all keys are CDATA strings (even if
          you provide a number for a key, internally it's sorted and treated as a
          string).
          Now welcome to JavaScript: as Array extends Object, it can be also used
          as a hash. So you can do something like:

          var arr = new Array();
          // add to array, arr.length == 1
          arr[0] = 10;

          // add new property (key/value pair)
          // arr.length is *not* affected !
          arr['foo'] = 'JavaScript is funny sometimes';

          Form values always returned to function as strings.
          So in a situation like
          arr[myForm.myField. value] = 4; // say myForm.myField. value == 17
          JavaScript cannot determine what do you want from it: whether
          you want to add new property called "17" or you want
          to add an array element with index 17.


          So to work securely with *array* you should do something like:

          var arr = new Array();
          ....
          var i = Math.parseInteg er(myForm.myFie ld.value, 10);
          if (i) {arr[i] = quantity;}

          This is with a value check.
          To skip on value check you can do runtime typisation by prefixing value
          with "+" (script will try to convert the following expression into a
          number):

          app[+myForm.myField .value] = quantity;

          Then later:
          for (i=0; i<arr.length; i++) {
          // check arr[i]
          }


          If you want to use hash (say using item names as your keys), you better
          use the generic Object() constructor to have you hash free from
          inherited properties.

          var hash = new Object();
          hash[key] = someValue;

          The later:

          for (key in hash) {
          // check hash[key]
          }

          Comment

          • VK

            #6
            Re: javascript arrays 2nd try

            > I beleive I have made a similar javascript array

            What is that new trend to mix array and hash in one term? No one calls
            integer as float or class as object. But array and hash - like no
            problem... Is it some new stuff from Computer Science university
            department?

            If you are doing *array", then you have to use only positive integer
            values for array index, as it was since ALGOL.

            Hash (Associative array) doesn't exists in JavaScript as a separate
            programming entity. But each object inherits internal hash mechanics
            from Object() constructor. In hash all keys are CDATA strings (even if
            you provide a number for a key, internally it's sorted and treated as a
            string).
            Now welcome to JavaScript: as Array extends Object, it can be also used
            as a hash. So you can do something like:

            var arr = new Array();
            // add to array, arr.length == 1
            arr[0] = 10;

            // add new property (key/value pair)
            // arr.length is *not* affected !
            arr['foo'] = 'JavaScript is funny sometimes';

            Form values always returned to function as strings.
            So in a situation like
            arr[myForm.myField. value] = 4; // say myForm.myField. value == 17
            JavaScript cannot determine what do you want from it: whether
            you want to add new property called "17" or you want
            to add an array element with index 17.


            So to work securely with *array* you should do something like:

            var arr = new Array();
            ....
            var i = Math.parseInteg er(myForm.myFie ld.value, 10);
            if (i) {arr[i] = quantity;}

            This is with a value check.
            To skip on value check you can do runtime typisation by prefixing value
            with "+" (script will try to convert the following expression into a
            number):

            app[+myForm.myField .value] = quantity;

            Then later:
            for (i=0; i<arr.length; i++) {
            // check arr[i]
            }


            If you want to use hash (say using item names as your keys), you better
            use the generic Object() constructor to have you hash free from
            inherited properties.

            var hash = new Object();
            hash[key] = someValue;

            The later:

            for (key in hash) {
            // check hash[key]
            }

            Comment

            Working...