javascript arrays 2nd try

M

meltedown

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=>$items){

//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.value]=qty.value;

But how do I iterate through this array ?
 
J

Jc

meltedown said:
But how do I iterate through this array ?

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/d...56/html/js56jslrfJScriptLanguageReference.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.
 
M

meltedown

Jc said:
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/d...56/html/js56jslrfJScriptLanguageReference.asp
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 ?
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.

I don't see what the problem is. The user can change the number of
items, but not the price.
 
J

Jc

meltedown said:
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 ?

This should be answered in your first post on this topic.
I don't see what the problem is. The user can change the number of
items, but not the price.

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.
 
V

VK

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.parseInteger(myForm.myField.value, 10);
if (i) {arr = 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
}


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]
}
 
V

VK

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.parseInteger(myForm.myField.value, 10);
if (i) {arr = 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
}


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]
}
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,744
Messages
2,569,483
Members
44,903
Latest member
orderPeak8CBDGummies

Latest Threads

Top