Click to See Complete Forum and Search --> : New to Javascript need a bit of help
Negduke
11-20-2003, 03:42 AM
Lo All
I have this problem and i need to create a Javascript program to solve it, however i only know basic Javascript. This is part of my course and i could do with a few pointers as to how to solve it.
----------------------------------------------------------------------------------
I need to create a Sales Analysis Tool, nothing to in depth.
The input from the user will be
- Sales Persons ID
- Quarter of the year in which sale took place (1-4)
- Sales Amount
I then need to create the following Algorithms
- Scan Sales posting ( get the input from user)
- Return a value to show success or failure or data scan (Could be data has been entered successfully etc )
- Compute Salesperson Totals
- Compute Quarterly Totals
- Display Sales Tables along with Salesperson totals and quarterly totals.
I need to make use of
- Functions
- One dimensional arrays
-----------------------------------------------------------------------------------
I know its a lot to ask but as i have not done much Javascript before im a bit lost, i have even asked a college but they are not very helpful. Any help is greatly appreciated.
Cheers
TheBearMay
11-20-2003, 09:59 AM
Let's see if I can push you in the right direction. I'll be making a few assumptions so stop me if I'm wrong.
#1 Easiest way to call a function is to have the user initiate it, i.e push a button or click on a link.
<button onclick="success=Myfunction();>Call Function</button>
#2 Assume the arrays are for collecting Sales Data. Easiest way is to set up a row of text boxes and name them for later processing.
<input type='text' size=??? name='SalesID'/>
#3 Call the function to store the data (see #1). Function will use something like array.push(SalesID.value) to add values to the array. (Easiest way here is to make the array a global variable.) Include the line: return true;
#4 After accumulating the data call another function to output the totals. A for loop works good here.
for(i=0;i<array.length-1;i++){
SalesTot=+array[i];
...
}
Negduke
11-20-2003, 11:00 AM
Is this using just one, one dimensional array? Sorry for the questions im a newbie :)
For instance
SalesID, Quarterly, Sales Amount
All in one Array, allowing me to add up totals by SalesID and those Quarterly Totals then displaying bothy seperately.
TheBearMay
11-20-2003, 11:18 AM
Assuming the SalesID can be numeric, it can be done using 1, 1D array for storing the data (position 1 of each data set is the ID, position 2 the quarter, position 3 the amount), and possibly 2 arrays to accumulate totals - 1 by salesperson, and 1 by quarter.
(I'm doing the code here on the fly so I may make a couple of typos.:rolleyes: )
...
for(i=0;i<array.length-1;++i){
HoldID=array[i];
i++;
HoldQtr=array[i];
i++;
SalesArray[HoldId]=+array[i];
QtrArray[HoldQtr]=+array[i];
}
Negduke
11-20-2003, 11:37 AM
Yes the SalesID will be numeric, i think im getting there just need something to click in my mind :)
So one Array to hold the info
Sales ID | Quarterly | Sales Amount
--------------------------------------------
344 | 3 | 23.45
23 | 2 | 10.34
Then 2 Arrays to Add up Sales ID totals and Quarterly Totals
Array to add up Sales Amount by Sales ID
Sales ID | Quarterly | Sales Amount
--------------------------------------------
344 | 3 | 23.45
344 | 2 | 11.23
Array to add up Sales Amount by Quarterly
Sales ID | Quarterly | Sales Amount
--------------------------------------------
344 | 2 | 28.45
23 | 2 | 11.23
Its just a bit confusing for myself as im new to it, just need to know how exactly the info gets from the Array holding the info and split in to the corresponding array (i.e. total by sales id or quarterly array) and then adding up the sales amount for each array and displaying.
Sorry if im confusing you :)
TheBearMay
11-20-2003, 01:33 PM
More like:
DataArray=(1, 3, 23.45, 2, 2, 10.34, 1, 2, 11.23....
(If you keep the ID numbers low and in sequence you'll have fewer array entries to wade through.)
The other two arrays are just accumulators:
Lets say that Q1 we have $200 in sales, Q2 $400, Q3 $600, Q4 $2400 then since we are using Zero Based arrays we have:
QtrArray=(0, 200.00, 400.00, 600.00, 2400.00)
The Sales array using the data above would end up looking similiar to:
SalesArray(0, 34.68, 10.34...
where Salesperson 1 has $34.68 total sales, Salesperson 2 has $10.34 total sales....
Negduke
11-20-2003, 02:03 PM
Last question then i will stop bugging you ;)
Could you give me an example piece of code to show how to set the Arrays up, and how i can take the user input (sales id, quarterly, sales amount) and put it in the data array. :)
Cheers for the help, just this bit to sort out and i think i have got it :)
Negduke
11-21-2003, 04:01 AM
I have done this so far to take the user input but i dont know if i have done it correctly probably not :(
<HTML>
<HEAD>
<TITLE>Assigment</TITLE>
<SCRIPT TYPE="text/javascript" LANGUAGE="JavaScript">
var data = new Array(3);
var count;
var salesid, quarterly, salesamount;
var index;
//set up array
data[0] = "salesid";
data[1] = "quarterly";
data[2] = "salesamount";
for (count=1; count<=3; count++) {
salesid = window.prompt ("Enter in Sales ID : ","");
quarterly = window.prompt ("Enter in Quarterly Seasaon : ","");
salesamount = window.prompt ("Enter in Sales Amount : ","");
document.write("Sales ID : ",salesid," Season : ",quarterly," Sales Amount : ",salesamount,"<br>");
}
</script>
</head></html>
TheBearMay
11-21-2003, 12:20 PM
That code will definitely get the input for you, but your data array will only include the text literals. Inside your for loop you may want to insert
data.push(salesid, quarterly, salesamount);
This will add the values of the collected data to the array.
As an aside, you may want to use either lower or upper case in your opening and closing tags, i.e. <HEAD>...</head>