...

View Full Version : How to add 2 seperate elements in 2 seperate arrays?



PembsPanther
07-27-2011, 07:07 PM
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:


// candidates
var candidateArray =
['Mr R Green...', 'Ms O Brown...', 'Ms Y Black...', 'Mr G White...', 'Ms B Grey....','Ms I Blue....', 'Mr V Pink....'];
// online votes
var onlineVotesArray = [21,47,23,11,56,47,30];
// paper votes
var paperVotesArray = [12,4,20,11,5,4,17];
//total votes
var totalVotesArray = (candidateArray.length);


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.

Thanks in advance :)

mathewfarrell35
07-27-2011, 07:15 PM
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

Philip M
07-27-2011, 07:20 PM
Here you are:-


<script type = "text/javascript">
// online votes
var onlineVotesArray = [21,47,23,11,56,47,30];
// paper votes
var paperVotesArray = [12,4,20,11,5,4,17];
//total votes
var totalVotesArray = [];
for (var i =0; i < onlineVotesArray.length; i++) { // or candidateArray.length
totalVotesArray[i] = onlineVotesArray[i] + paperVotesArray[i];
}
alert (totalVotesArray); // 33,51,43,22,61,51,47
</script>


There is no need to assign a length to the totalVotesArray, but if your tutor requires that use
var totalVotesArray = new Array(7);
or var len = candidateArray.length;
var totalVotesArray = new Array(len);
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.

PembsPanther
07-27-2011, 07:26 PM
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?

Thanks in advance.

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:

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

Then for the addition part:


{
totalVotesArray[i] = onlineVotesArray[i] + paperVotesArray[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 :(

PembsPanther
07-27-2011, 07:28 PM
Here you are:-


<script type = "text/javascript">
// online votes
var onlineVotesArray = [21,47,23,11,56,47,30];
// paper votes
var paperVotesArray = [12,4,20,11,5,4,17];
//total votes
var totalVotesArray = [];
for (var i =0; i < onlineVotesArray.length; i++) { // or candidateArray.length
totalVotesArray[i] = onlineVotesArray[i] + paperVotesArray[i];
}
alert (totalVotesArray); // 33,51,43,22,61,51,47
</script>



There is no need to assign a length to the totalVotesArray, but if your tutor requires that use
var totalVotesArray = new Array(7);

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??

Philip M
07-27-2011, 07:39 PM
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).

PembsPanther
07-27-2011, 07:55 PM
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

Old Pedant
07-28-2011, 02:21 AM
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.]

Philip M
07-28-2011, 07:28 AM
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 ;))



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum