0

i used this code to adding multiple values.. bt it dsnt work . it didnt give me real answer.. plz some one can help me up..

function calc(y){

answer="0";
for(var co=1;co<=y;co++){
answer+=parseInt(document.getElementById(co).value);
}
document.getElementById("total").value=answer;
}

this code didnt give me sum of values. it gave like this.. (10101010). not of some of values. plz help me..

6
Contributors
7
Replies
8
Views
7 Years
Discussion Span
Last Post by dhanapal86mca
0

First of all the output you gave (10101010) is not correct in fact according to your naked (uncommented) code the output should start with "0" as you have declared answer as string(No idea why).

Put some clothes (comments) in your code. We can hardly make sense about this code.

0

Ok, looking at your code, it is difficult to see what you are trying to calculate - so this is the thinking process I am using to figure out the possible code.

You have already set the answer to 0 in your formula and do not reset the value anywhere. This keeps the value of answer to 0 without changing it.

The statement looks off, first I don't know what the value of y is but lets assume it is already stated in the code. I usually write it this way

for (i=0, i<y.length, i++)

On line #5, you have

answer+=parseInt(document.getElementById(co).value

It seems this is where you are wanting the calculation, however it is written where you are adding the parseInt information to the value already assigned to the variable of answer.

What I find helps me is I make a variable for each value I am reading from the page. For me this keeps the information clear and instead of going through the document.getElementById statements I can look at the variables.

var answer;
var val1 = document.getElementById("co").value;
// The line below is to represent where the other value that you are adding to ("co")
var val2 = document.getElementById("co2").value;
answer = val1+val2;

Then when you have

document.getElementById("total").value=answer;

The variable answer will have the correct value.

As the code stands now - it seems you are starting at the second value in the calculation (since you have it set at 1 and arrays start with 0) so the function is not getting the sum, it is adding the values together as if they were text and starting with 1.

0

didnt give me real answer

Change line 3 to

answer=0;

and at least your code will start doing arithmetic.

Unfortunately there are other problems.

Edited by fxm: n/a

0

thnx for helping. anywy i did like this . now itz working.
i changed 5th line into this.

answer-=-parseInt(document.getElementById(co).value);

still cant figure out why cant use "+" this for calculation..

0

still cant figure out why cant use "+" this for calculation..

The "+" operator has either of two functions: addition or concatenation.
By mis-defining 'answer' as a string, you caused javascript to use the latter (which is not what you want for this appication).

0

Hi Kadafiz,

If you are going to use the value with double quotes then probably you can use eval to achieve the result:

<!doctype html public "-//W3C//DTD HTML 4.0 Transitional//EN">
<html>
<head>
<title> New Document </title>
<meta name="Generator" content="EditPlus">
<meta name="Author" content="">
<meta name="Keywords" content="">
<meta name="Description" content="">
</head>

<body>
<script language="javascript">
function calc(y){
answer=eval("0");
for(var co=1;co<=y;co++){
answer+=parseInt(document.getElementById(co).value);
}
document.getElementById("total").value=answer;
}
</script>
<input type="text" id="total" name="tot"/>
<input type"text" id="1" value="10"/>
<input type"text" id="2" value="20"/>
<input type"text" id="3" value="30"/>
<input type="submit" value="submit" onClick="calc(3)"/>
</body>
</html>
This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.