Hello and welcome to our community! Is this your first visit?
Enjoy an ad free experience by logging in. Not a member yet? Register.

# Thread: How to add 2 seperate elements in 2 seperate arrays?

1. ## How to add 2 seperate elements in 2 seperate arrays?

Hello everyone

I have just found this wonderful forum through Dr Google whilst looking for an answer to my incredibly frustrating question! I am an OU student it is for an assignment I just cannot find the answer in any of the books and searching the net has got me nothing but so far wasted 3 hours
Here is the code snippet:

Code:
```// candidates
var candidateArray =
['Mr R Green...', 'Ms O Brown...', 'Ms Y Black...', 'Mr G White...', 'Ms B Grey....','Ms I Blue....', 'Mr V Pink....'];
First I had to add a new array called totalVotes and assign a length to it the same as candidateArray, I have done this in the last 2 lines I think this is correct, the question did stipulate that if the candidate array was changed then so would the total votes array hence linking it via length.

Now here is where I am at a complete loss The exact wording is as follows:

Use a for loop to calculate the total votes for each candidate according to the following structured english:

for each array position

add the element at that position in the online vote array to the element at the position in the paper vote array.

store the result at the corresponding position in the total votes array

end for

I have been scratching my head for 3 hours now, I have to have this assignment uploaded before midnight to my tutor or I am in the poo Any help really really appreciated.

• use pointers to ddo this. as the pointers are reffering the address of a location they suits you best for your purpose try it once

• ## Users who have thanked mathewfarrell35 for this post:

PembsPanther (07-27-2011)

• Here you are:-

Code:
```<script type = "text/javascript">
for (var i =0; i < onlineVotesArray.length; i++) {  // or candidateArray.length
}
</script>```

There is no need to assign a length to the totalVotesArray, but if your tutor requires that use
or var len = candidateArray.length;
But the preferred syntax is var totalVotesArray = [];
If additional values are placed in the array (further candidates) it will automatically expand to suit.

So which candidate scored the most votes in total? Loop through the array again to find the highest value (61) and then display the corresponding name.

If you are 20 and you are not a socialist, then you have no heart. If you are 40 and you are still a socialist, then you have no brain.

• ## Users who have thanked Philip M for this post:

PembsPanther (07-27-2011)

• Originally Posted by mathewfarrell35
use pointers to ddo this. as the pointers are reffering the address of a location they suits you best for your purpose try it once
Hello

Thanks for your reply sorry I am not familiar with "pointers" ? Sorry to come acroos as such a pleb it is a basic javascript course and my first run in with any kind of programming Brain seems to have gone dead the older I get just getting info in it is a nightmare!

Could you elaborate a little?

PS:

I have found the answer on the internet looks lilk one of my fellow students did not get it either, I have the code all working now trouble is I don't want to use it yet as I dont understand it

The code is:
Code:
`for (var i = 0; i < candidateArray.length; i++)`

{
}
Sorry this probably seems really simple to you guys but I am struggling been at it solid for 3 days now think I have fried my brain a little, I know I should understand this I just don't

• Originally Posted by Philip M
Here you are:-

Code:
```<script type = "text/javascript">
for (var i =0; i < onlineVotesArray.length; i++) {  // or candidateArray.length
}
</script>```

There is no need to assign a length to the totalVotesArray, but if your tutor requires that use

If you are 20 and you are not a socialist, then you have no heart. If you are 40 and you are still a socialist, then you have no brain.

I did do that at first but changed it to the candidateArray.length because the question said if the candidateArray is changed it must automatically reflect this in the totalVoteArray??

• Originally Posted by PembsPanther
I did do that at first but changed it to the candidateArray.length because the question said if the candidateArray is changed it must automatically reflect this in the totalVoteArray??
That is fine, but as I say it is not necessary to specify the length as the array totalVoteArray will expand in length automatically if further candidates are added.

As I say, current best practice is to avoid the "new" keyword, and use the square bracket notation
var totalVotesArray = []; You are using the square bracket notation already, so it makes better sense to use that same syntax to define the totalVotesArray.

In computer science, a pointer is a programming language data type whose value refers directly to (or "points to") another value stored elsewhere in the computer memory using its address. For example, onlineVotesArray[0] refers or points to the value 21. (Remember that the array index starts at 0).

• ## Users who have thanked Philip M for this post:

PembsPanther (07-27-2011)

• Originally Posted by Philip M
That is fine, but as I say it is not necessary to specify the length as the array totalVoteArray will expand in length automatically if further candidates are added.

As I say, current best practice is to avoid the "new" keyword, and use the square bracket notation
var totalVotesArray = []; You are using the square bracket notation already.
Hi Phillip

Thanks for the reply I do appreciate it

TBH this course is the last presentation it has just been replaced with a more up to date version (shame I did not know about that when I handed my £500 over!!!!) hence why some things are probably a bit out of date now

Never mind onwards and upwards

Thanks again

• Pembs: You should be aware that mathew's answer isn't really appropriate terminology for JavaScript. We don't normally talk about pointers in JavaScript; they tend to be used in C and C++ languages.

Philip's explanation is as close as you can come to the concept of pointers in JavaScript, since you can't directly use or manipulate pointers from JavaScript. But even Philip's terminology isn't what you would normally see with JavaScript.
For example, onlineVotesArray[0] refers or points to the value 21.
"refers to", yes. And indeed we talk about "references" (e.g., especially "object references") in JavaScript (and Java) all the time. But "points to"...ehhh...no, not really.

So don't try to wrap your head around pointers. It will just make you dizzy.

[At the risk of further earning my "pedant" name, I'll say that onlineVotesArray[0] would indeed be a valid pointer in C/C++. But technically it would then be referring to the block of memory occupied by that array element. It wouldn't "point to the value" at all. You would have to "dereference the pointer" to obtain the value. See? I said it would make you dizzy.]

• Originally Posted by Old Pedant
Pembs: You should be aware that mathew's answer isn't really appropriate terminology for JavaScript. We don't normally talk about pointers in JavaScript; they tend to be used in C and C++ languages.

Philip's explanation is as close as you can come to the concept of pointers in JavaScript, since you can't directly use or manipulate pointers from JavaScript. But even Philip's terminology isn't what you would normally see with JavaScript.

"refers to", yes. And indeed we talk about "references" (e.g., especially "object references") in JavaScript (and Java) all the time. But "points to"...ehhh...no, not really.

So don't try to wrap your head around pointers. It will just make you dizzy.
Absolutely right, but this is a good example of pitching an explanation at the level of the enquirer who is asking the question, and not at the level of the person answering the question. That is fundamantal to good teaching in my view. As you say, a full and accurate explanation of pointers would just make him dizzy. (It makes me dizzy as well )

•

#### Posting Permissions

• You may not post new threads
• You may not post replies
• You may not post attachments
• You may not edit your posts
•