- Home /
Off-topic
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!
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
Answer by Wolfram · Jun 02, 2011 at 10:51 AM
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];
Follow this Question
Related Questions
From C# to C for the S2 game engine 4 Answers
unity web player startup 0 Answers
blender tutorials 1 Answer
Why is Unity so great? 2 Answers
Problems with Webplayer 0 Answers