Closed Thread Icon

Topic awaiting preservation: Array and calculation problem (Page 1 of 1) Pages that link to <a href="https://ozoneasylum.com/backlink?for=8725" title="Pages that link to Topic awaiting preservation: Array and calculation problem (Page 1 of 1)" rel="nofollow" >Topic awaiting preservation: Array and calculation problem <span class="small">(Page 1 of 1)</span>\

 
Hiroki
Paranoid (IV) Inmate

From: NZ
Insane since: Dec 2002

posted posted 06-18-2003 23:50

Hi, guys. How are you? Long time no see on here.
I have been doing database assingment so that I cannot have time for JavaScript.
Now I am back to work now.

Well, I have been trying to figure out how to use Array. My understanding is not solid yet. Array is sort of collection of two or more adjacent memory cells that are associated iwth a single name, right?

Now I am scripting an exercise and having two problems.

My task is:

quote:
You have been asked to write a program for a sales person who has to hand in a 6 monthly return to the boss showing the sales reuslts for each of the 6 months.

Once the data has been entered into an array, your customer wants to see:

The total sales for the 6 month period.
The average sales for the 6 month period.
The difference from the average for each of teh 6 months




My uploaded file is here.

My problem is:

1. I want to show word for month instead of using number. ex. Jan not 1.
2. The difference from the average for each of the 6 months is not right.

Actually I am not sure where to put average fomular. average = total / 6, isn't it? A friend of mine told me not to put this inside of the loop. Then I put it outside of the loop. Still not working. Hmm....Would you see anything wrong???
Hiroki Kozai

[This message has been edited by Hiroki (edited 06-18-2003).]

[This message has been edited by Hiroki (edited 06-18-2003).]

Dracusis
Maniac (V) Inmate

From: Brisbane, Australia
Insane since: Apr 2001

posted posted 06-19-2003 02:14

Well, everything seems to be working just fine in your uploaded file, but since you mentioned you were having problems understanding arrays I thought I'd try and shed some light here.

I always seemed to have problems understanding arrays at first, but a friend of mine explained them in a really simple way that helped a lot. All the programming text books I've read always seem to use metaphors that didn't really fit what they were trying to explain.

If you think of a variable as a car, say we have the variable Holden and it contains the string "Station Wagon, Red, 1997". Now, an array is a special kind of variable but if a standard variable is a car, then an array is like train.

Say you have the array GoodsTrain, then you have a way to store variables for each carrage of that train. GoodsTrain[0] might be "Engine", GoodsTrain[3] (the third carriage) might be "coal" or some such, GoodsTrain[GoodsTrain.length-1] (length -1 because arrays start at 0) will probably be the caboose.

The other kind of array is an associative array, which is like the train but you name each carriage and refer to it that way as opposed to referring to its carriage number. So, GoodsTrain['coalCarriage'] might then hold the value of "300kg", telling you how much coal that carriage holds.

As you can see, an array can be used for a million and one different things. The type of array you use the kind of data you put into it will depend on what your trying to do.

Anyways, I hope that helps clear things up instead of confusing you even more.

Hiroki
Paranoid (IV) Inmate

From: NZ
Insane since: Dec 2002

posted posted 06-19-2003 07:06

Hi, Dracusis. Thanks a lot.
Yes, it is great way to explain about Array, isn't it?
I solved one of my problems now!
But still I cannot figure out how to show months in words instead of using number.
When you run the program, you will input all the temperature from Jan ~ June.
Then you will get print out of result.
When you see it, you will see 0,1,2,3,4,5 on the month's column. I want to show Jan, Feb, Mar, Apr, May, June.
What I did is:

code:
var month = new Array("Jan", "Feb", "Mar", "Apr", "May", "Jun");
var total = 0;
var ave=0;
for(i=0; i<=5; i++){
month[i]=(parseFloat(prompt("Please enter the sales of " + month[i])));
total=total+month[i];
diff=ave-month[i];
document.write(i + "--------------");
document.write(month[i] +"-----");
document.write(diff);
document.write("<br />");
}



Hmmm.....But still doesn't work. Would you see why???

Hiroki Kozai

Dracusis
Maniac (V) Inmate

From: Brisbane, Australia
Insane since: Apr 2001

posted posted 06-19-2003 07:57

THat's because your re-assining the values in the array that contains the month names.

Here: month[i]=(parseFloat(prompt("Please enter the sales of " + month[i])));

The value of month[i] shows as "jan" or "feb" etc... in the prompt but when you select "OK" on that prompt it's replacing that value with the value entered in the prompt.

So if i = 0 then month[i] = "jan" but you're then assigning month[i] to the prompt value so it's no longer "jan" isn't now the number the user entered.

If you add in another array to keep track of the prompt data then all should be fine. Infact, I'm not even sure if it needs to be an array, you should be able to write over the prompt value every time and it shouldn't make any difference so it could probably just be an ordaniary vraiable but I've used an array in the example below just incase:

code:
var promptArray = []  // Quick'n'dirty way to make a null array variable

for(i=0; i<=5; i++){
promptArray [i] = ( parseFloat( prompt( "Please enter the sales of " + month[i] ) ) );
total = total + promptArray[i];
document.write( month[i] + "--------------" + promptArray[i] +"-----" + ave - promptArray[i];+ "<br />" );
}



Hiroki
Paranoid (IV) Inmate

From: NZ
Insane since: Dec 2002

posted posted 06-20-2003 04:34

Hi, Dracusis.
Many many thanks for your replies.
It worked fine now.
Array.....I need to code more examples....
But much more confident than before.
Again, Many thank you, mate.
Cya.

Hiroki Kozai

« BackwardsOnwards »

Show Forum Drop Down Menu