473,378 Members | 1,412 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,378 software developers and data experts.

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=>$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[i].value]=qty[i].value;

But how do I iterate through this array ?
Jul 23 '05 #1
5 1976
Jc
meltedown wrote:
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/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.

Jul 23 '05 #2
Jc wrote:
meltedown wrote:
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/de...eReference.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.

Jul 23 '05 #3
Jc
meltedown wrote:
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.
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.


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.

Jul 23 '05 #4
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[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]
}

Jul 23 '05 #5
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[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]
}

Jul 23 '05 #6

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

4
by: annoyingmouse2002 | last post by:
Hi there, sorry if this a long post but I'm really just starting out. I've been using MSXML to parse an OWL but would like to use a different solution. Basically it reads the OWL (Based on XML)...
13
by: Kevin | last post by:
Help! Why are none of these valid? var arrayName = new Array(); arrayName = new Array('alpha_val', 1); arrayName = ; I'm creating/writing the array on the server side from Perl, but I
22
by: VK | last post by:
A while ago I proposed to update info in the group FAQ section, but I dropped the discussion using the approach "No matter what color the cat is as long as it still hounts the mice". Over the last...
35
by: VK | last post by:
Whatever you wanted to know about it but always were affraid to ask. <http://www.geocities.com/schools_ring/ArrayAndHash.html>
1
by: Alfredo Magallón Arbizu | last post by:
Hello, I need to pass the contents of a dataset to the client in order to use these contents with javascript. What is the best way to achieve that? Normally I use controls like a grid or...
104
by: Leszek | last post by:
Hi. Is it possible in javascript to operate on an array without knowing how mamy elements it has? What i want to do is sending an array to a script, and this script should add all values from...
41
by: Rene Nyffenegger | last post by:
Hello everyone. I am not fluent in JavaScript, so I might overlook the obvious. But in all other programming languages that I know and that have associative arrays, or hashes, the elements in...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
by: ryjfgjl | last post by:
In our work, we often need to import Excel data into databases (such as MySQL, SQL Server, Oracle) for data analysis and processing. Usually, we use database tools like Navicat or the Excel import...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.