PDA

View Full Version : Array comparing and calculating


Jerome
07-27-2003, 12:15 PM
Hi,

Given:

I have 2 array's filled with numbers. These arrays can be different in length each time, but compared to each-other they have an equal length!

Example:

A1=new Array(14,35,27,87);
A2=new Array(13,42,34,79);

Questions:

I like to have a new array with the highest number on each place, the results should look like:

A3=new Array(14,42,34,87);

After that I like to have the sum of the values from that array, in other words:

177

Thanks for Your effort and free time!
Jerome

boywonder
07-27-2003, 03:37 PM
You could run a loop and compare the two arrays at each index.
What have you tried to this point?

scriptkeeper
07-27-2003, 04:11 PM
Give this a whirl youll have to do the addition your self Im late 4 work sorry Good Luck!

<html>
<head>
<script type="text/javascript">
var A1=[20,80,60,90,150];
var A2=[50,70,40,120,180];
var A3=[];
for(var i=0;i<A1.length;i++){
A3[i]=(A1[i]>A2[i])?A1[i]:A2[i];
}
</script>
</head>
<body onload="alert(A3);">
</body>
</html>

Jerome
07-27-2003, 08:34 PM
Thanks guys!

Instead of building two arrays, which I then could compare, I directly make one array with the values I need and use the same loop to have the sum of the values!

Have a nice week-end,
Jerome