fredsmith

01-25-2009, 12:00 PM

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.:confused:

thanks fred.

oesxyl

01-25-2009, 12:04 PM

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.:confused:

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/globalfunctions.shtml

best regards

Philip M

01-25-2009, 12:20 PM

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>

brandonH

01-25-2009, 12:30 PM

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)

Philip M

01-25-2009, 12:46 PM

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;

}

oesxyl

01-25-2009, 01:19 PM

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>

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)

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

fredsmith

01-25-2009, 02:16 PM

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 :thumbsup: