Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 9 of 9
  1. #1
    New to the CF scene
    Join Date
    Jul 2011
    Posts
    4
    Thanks
    3
    Thanked 0 Times in 0 Posts

    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....'];
    // 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

  • #2
    New to the CF scene
    Join Date
    Jul 2011
    Posts
    5
    Thanks
    0
    Thanked 1 Time in 1 Post
    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)

  • #3
    Supreme Master coder! Philip M's Avatar
    Join Date
    Jun 2002
    Location
    London, England
    Posts
    17,907
    Thanks
    203
    Thanked 2,531 Times in 2,509 Posts
    Here you are:-

    Code:
    <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.
    Last edited by Philip M; 07-27-2011 at 07:34 PM.

    All the code given in this post has been tested and is intended to address the question asked.
    Unless stated otherwise it is not just a demonstration.

  • Users who have thanked Philip M for this post:

    PembsPanther (07-27-2011)

  • #4
    New to the CF scene
    Join Date
    Jul 2011
    Posts
    4
    Thanks
    3
    Thanked 0 Times in 0 Posts
    Quote Originally Posted by mathewfarrell35 View Post
    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:
    Code:
    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

  • #5
    New to the CF scene
    Join Date
    Jul 2011
    Posts
    4
    Thanks
    3
    Thanked 0 Times in 0 Posts
    Quote Originally Posted by Philip M View Post
    Here you are:-

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

  • #6
    Supreme Master coder! Philip M's Avatar
    Join Date
    Jun 2002
    Location
    London, England
    Posts
    17,907
    Thanks
    203
    Thanked 2,531 Times in 2,509 Posts
    Quote Originally Posted by PembsPanther View Post
    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).
    Last edited by Philip M; 07-27-2011 at 07:58 PM.

    All the code given in this post has been tested and is intended to address the question asked.
    Unless stated otherwise it is not just a demonstration.

  • Users who have thanked Philip M for this post:

    PembsPanther (07-27-2011)

  • #7
    New to the CF scene
    Join Date
    Jul 2011
    Posts
    4
    Thanks
    3
    Thanked 0 Times in 0 Posts
    Quote Originally Posted by Philip M View Post
    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

  • #8
    Supreme Master coder! Old Pedant's Avatar
    Join Date
    Feb 2009
    Posts
    25,111
    Thanks
    75
    Thanked 4,335 Times in 4,301 Posts
    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.]
    Last edited by Old Pedant; 07-28-2011 at 02:23 AM.

  • #9
    Supreme Master coder! Philip M's Avatar
    Join Date
    Jun 2002
    Location
    London, England
    Posts
    17,907
    Thanks
    203
    Thanked 2,531 Times in 2,509 Posts
    Quote Originally Posted by Old Pedant View Post
    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 )

    All the code given in this post has been tested and is intended to address the question asked.
    Unless stated otherwise it is not just a demonstration.


  •  

    Posting Permissions

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