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 7 of 7
  1. #1
    New to the CF scene
    Join Date
    Jan 2009
    Posts
    2
    Thanks
    5
    Thanked 0 Times in 0 Posts

    Question about adding votes in JavaScript

    Hi all
    I am wondering if anyone can help me as I am at the stage of "pulling my hair out".I have been set a task of the following:code a javascript html program
    to have a user input the votes cast for 5 candidates, these votes should then be added to a previous postal vote already declared,then work out the total votes cast the totalmembership and percentage each candidate had for both.
    my biggest problem in understanding this question is combining the candidate vote with the postal vote?eg if the candidate had 500 postal votes and the input was a further 200 votes the result should be 700 each time I do this Iget
    500700 as the result. I dont expect the answer to be given for me but could anyone please give me some examples of how to do such a problem.

    thanks fred.

  • #2
    Master Coder
    Join Date
    Dec 2007
    Posts
    6,682
    Thanks
    436
    Thanked 890 Times in 879 Posts
    Quote Originally Posted by fredsmith View Post
    Hi all
    I am wondering if anyone can help me as I am at the stage of "pulling my hair out".I have been set a task of the following:code a javascript html program
    to have a user input the votes cast for 5 candidates, these votes should then be added to a previous postal vote already declared,then work out the total votes cast the totalmembership and percentage each candidate had for both.
    my biggest problem in understanding this question is combining the candidate vote with the postal vote?eg if the candidate had 500 postal votes and the input was a further 200 votes the result should be 700 each time I do this Iget
    500700 as the result. I dont expect the answer to be given for me but could anyone please give me some examples of how to do such a problem.

    thanks fred.
    I'm guessing,
    500 and 700 are strings and you use +, that means concatenation. use parseInt to get a number from a string and then add them using +:
    http://www.javascriptkit.com/jsref/g...unctions.shtml

    best regards

  • Users who have thanked oesxyl for this post:

    fredsmith (01-25-2009)

  • #3
    Supreme Master coder! Philip M's Avatar
    Join Date
    Jun 2002
    Location
    London, England
    Posts
    17,730
    Thanks
    202
    Thanked 2,508 Times in 2,486 Posts
    if the candidate had 500 postal votes and the input was a further 200 votes the result should be 700 each time I do this Iget
    500700 as the result.
    That suggests a further problem something like:-

    <script type = "text/javascript">
    var postal = "500"; // string
    var postalVotes = 500 // same value as a number
    var votesInPerson = 200; // number
    var totalVotes = postalVotes + votesInPerson;
    var result = postal + totalVotes;
    alert (result); // 500700
    </script>

  • Users who have thanked Philip M for this post:

    fredsmith (01-25-2009)

  • #4
    Regular Coder
    Join Date
    Oct 2003
    Location
    on a ship
    Posts
    574
    Thanks
    1
    Thanked 6 Times in 5 Posts
    not that this is deemed to be the best method of doing what you are asking for, but it does work:



    when calculating (adding the two values together)

    you can use double negative(valueone-(-valuetwo)) instead of the + sign.

    or you can subrtract 0 from each value then add them together, which forces the variables into a math calculation, and javascript will try to convert it, if it can it does.
    (valueone-0)+(valuetwo-0)
    I make no attempt at pretending like I'm a professional. I offer help with what knowledge I do have.

  • The Following 2 Users Say Thank You to brandonH For This Useful Post:

    fredsmith (01-25-2009), oesxyl (01-25-2009)

  • #5
    Supreme Master coder! Philip M's Avatar
    Join Date
    Jun 2002
    Location
    London, England
    Posts
    17,730
    Thanks
    202
    Thanked 2,508 Times in 2,486 Posts
    Quote Originally Posted by brandonH View Post
    not that this is deemed to be the best method of doing what you are asking for, but it does work:



    when calculating (adding the two values together)

    you can use double negative(valueone-(-valuetwo)) instead of the + sign.

    or you can subtract 0 from each value then add them together, which forces the variables into a math calculation, and javascript will try to convert it, if it can it does.
    (valueone-0)+(valuetwo-0)

    Another slightly less ugly kludge is to multiply by *1.

    But if the variables are strings then the result is NaN

    var valueone = "x";
    var valuetwo = "y";
    var result1 = valueone + valuetwo;
    alert (result1); // xy
    var result2 = (valueone-0)+(valuetwo-0)
    alert (result2); // NaN

    You can trap this with:-

    if ((!isNaN(valueone)) && (!isNaN(valuetwo))) {
    result = valueone + valuetwo;
    }
    Last edited by Philip M; 01-25-2009 at 12:50 PM.

  • The Following 2 Users Say Thank You to Philip M For This Useful Post:

    fredsmith (01-25-2009), oesxyl (01-25-2009)

  • #6
    Master Coder
    Join Date
    Dec 2007
    Posts
    6,682
    Thanks
    436
    Thanked 890 Times in 879 Posts
    Quote Originally Posted by Philip M View Post
    That suggests a further problem something like:-

    <script type = "text/javascript">
    var postal = "500"; // string
    var postalVotes = 500 // same value as a number
    var votesInPerson = 200; // number
    var totalVotes = postalVotes + votesInPerson;
    var result = postal + totalVotes;
    alert (result); // 500700
    </script>
    Quote Originally Posted by brandonH View Post
    when calculating (adding the two values together)

    you can use double negative(valueone-(-valuetwo)) instead of the + sign.

    or you can subrtract 0 from each value then add them together, which forces the variables into a math calculation, and javascript will try to convert it, if it can it does.
    (valueone-0)+(valuetwo-0)
    Quote Originally Posted by Philip M View Post
    Another slightly less ugly kludge is to multiply by *1.

    But if the variables are strings then the result is NaN
    I guess that values come from a form and I understand both tricks so I have a question,
    it's something wrong with using parseInt in this case?

    best regards

  • Users who have thanked oesxyl for this post:

    fredsmith (01-25-2009)

  • #7
    New to the CF scene
    Join Date
    Jan 2009
    Posts
    2
    Thanks
    5
    Thanked 0 Times in 0 Posts

    Smile

    Thanks Everyone
    I will get to work on all your suggestions now! I am sure they will all work,Ive not been into the programming side of computers before and it is a bit of a minefield for my tiny brain, i was doing fairly well till someone decided we had to learn arrays etc lol . but thank you all for your help


  •  

    Posting Permissions

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