x


[Closed] JavaScript

Hi i wonder if anyone can help, I'm trying to write a program in JS to add the elements of two arrays together - ie firstArray = [1,2,3,4,5,6,7,8,9] secondArray = [2,3,4,5,6,7,8,9,10]...........I need to know how to add the first element in firstArray to the first element in secondArray and display the result.

I have the data in the arrays already and have writen this:-

var onlineVotesArray = new Array (41,37,43,11,59,21,36);
var paperVotesArray = new Array (22,3,15,11,7,1,10);
var candidateArray = new Array ("Mrs J Smith", " Mrs H J Rowe", " Ms Brown", " A N Other"," Mr White"," Ms I M Blue", " Mr P D Jones");
var totalVotesArray = new Array ();
    for (i = 0, l = candidateArray.length; i < l; i++)
    {
    document.write(candidateArray[i] + ' has ' + onlineVotesArray[i] + ' Online votes and ' + 
    paperVotesArray[i] + ' Paper votes giving them a total of ' + ' votes.' +'<BR>');
    }
    document.write('<BR>');

but I do not know how to add the elements together, I thought of putting:- totalVotesArray = ((onlineVotesArray) + (paperVotesArray)); but that does not work - help please!

more ▼

asked Jun 02 '11 at 10:40 AM

dobby gravatar image

dobby
1 1 1 1

This doesn't appear to be a Unity question, so next time I suggest asking it at http://stackoverflow.com/ which has the additional advantage of being a much larger community, so it is more likely you'll get more/quicker/better answers

Jun 02 '11 at 10:55 AM Wolfram
(comments are locked)
10|3000 characters needed characters left

The question has been closed Jun 02 '11 at 02:00 PM by Eric5h5 for the following reason:

Off-topic


1 answer: sort voted first

Before your for-loop initialize the array to have the correct size:

totalVotesArray.length=candidateArray.length;

In your for-loop just add the arrays on a per-element-basis:

totalVotesArray[i]=onlineVotesArray[i]+paperVotesArray[i];
more ▼

answered Jun 02 '11 at 10:51 AM

Wolfram gravatar image

Wolfram
9k 8 20 52

(comments are locked)
10|3000 characters needed characters left

Follow this question

By Email:

Once you sign in you will be able to subscribe for any updates here

By RSS:

Answers

Answers and Comments

Topics:

x52

asked: Jun 02 '11 at 10:40 AM

Seen: 973 times

Last Updated: Jul 18 '11 at 10:56 PM