JavaScript Arrays Further

I need more help…

I have the following:

var judgesPointsArray = [2,1,5,4,3];
var audiencePointsArray = [4,5,2,3,1];

I need to create a loop which adds together the variables but in a significant way.

I need it to add 2 + 4, 1 + 5, 5 + 2 etc…

I also need it to return which is the highest value is combined?

I hope this makes sense…

You can wrap this in a function to suit your needs, the “highest” variable will hold the highest added value:


var highest = 0, newArr = [];
for (var i = 0, j = judgesPoints; i < j; i++) {
  newArr.push(judgesPoints[i] + audiencePoints[i]);
  if (newArr[i] > highest) highest = newArr[i];
}

Think about how you name your variables. The longer and more verbose they are the less useful they become (to a point).

I cannot thank you guys enough for all your help, you are all so kind.

Excellent work and I regard you highly.

To break line you can use “<br>”
I changed them by using your arrays.



<script type="text/javascript">

var judgesPointsArray = [2,1,5,4,3];
var audiencePointsArray = [4,5,2,3,1];

var sum = [];
for(var i = 0; i < judgesPointsArray.length; i++) {
sum[i] = judgesPointsArray[i] + audiencePointsArray[i];
}

document.write(sum); 
var theHighestValue = sum[0];
for(var n = 1; n < sum.length; n++) {
if(theHighestValue < sum[n] ) { theHighestValue = sum[n]; }
}

document.write("<br>"+theHighestValue);

</script>


I have changed this so it writes the information and not alerts it, could it be possible to use my arrays that I initially placed instead of the new ones created by the person who kindly provided this code. It is perfect but need to have the arrays of how I initially created them. also, if you run this script it just puts everything on 1 line, could somebody advise how to break the line up as I have tried both of these things and it does not execute properly.

<script type=“text/javascript”>
var j = [2,1,5,4,3];
var a = [4,5,2,3,1];
var c = ;
for(var i = 0; i < j.length; i++) {
c[i] = j[i] + a[i];
}
document.write(c); 6, 6, 7, 7, 4
var h = c[0];
for(var n = 1; n < c.length; n++) {
if(h < c[n] ) { h = c[n]; }
}

document.write(h);

</script>

Hi Raffles:
j = judgesPoints; shouldn’t be j = judgesPoints.length; ?
or better
i<judgesPoints.length;
or Am I missing something?



<script type="text/javascript">

var j = [2,1,5,4,3];

var a = [4,5,2,3,1];

var c = [  j[0] + a[0], j[1] + a[1], j[2] + a[2], j[3] + a[3], j[4] + a[4]  ];
/*
    c = [   2   +   4 ,   1  +   5 ,   5   +  2 ,    4  +    3,    3  +  1   ];
     
    c = [       6     ,      6     ,       7    ,       7     ,       4      ];
*/

alert(c);   // 6, 6, 7, 7, 4

</script>


or

Make an array. Using for loop, assign values to it.



<script type="text/javascript">

var j = [2,1,5,4,3];

var a = [4,5,2,3,1];

var c = [];

for(var i = 0; i < j.length; i++) {

c[i] = j[i] + a[i];

}

alert(c); // 6, 6, 7, 7, 4

</script>


max(x,y,z,…,n) Returns the number with the highest value

How to use max() to return the number with the highest value of two specified numbers.

To return the number with the highest value of an array



<script type="text/javascript">

var c= [ 6, 6, 7, 7, 4 ]; 

var h = Math.max( c[0], c[1], c[2], c[3], c[4] );

alert(h);

</script>


Using for loop, to return the number with the highest value of an array



<script type="text/javascript">

var c= [ 6, 6, 7, 7, 4 ];  

var h = c[0];

for(var i = 1; i < c.length; i++) {

if(h < c[i] ) {  h = c[i]; }

}

alert(h);

</script>


To return the number with the highest value of an array.



<script type="text/javascript">

var c= [ 6, 6, 7, 7, 4 ]; 

var h = Math.max.apply( "",c );

alert(h);  // 7

</script>


The full code:



<script type="text/javascript">

var j = [2,1,5,4,3];

var a = [4,5,2,3,1];

var c = [];

for(var i = 0; i < j.length; i++) {

c[i] = j[i] + a[i];

}

alert(c); // 6, 6, 7, 7, 4

var h = c[0];

for(var n = 1; n < c.length; n++) {

if(h < c[n] ) {  h = c[n]; }

}

alert(h);

</script>