Hello and welcome to our community! Is this your first visit?
Enjoy an ad free experience by logging in. Not a member yet? Register.

1. sum of array values

Hi Guys,
How do I sum the values of an array and output the result using document.write?

Say my array is var number=new Array(1.234,56.43,1.02);

THANKS

Thought this wud work?

Code:
```<script type="text/javascript">

var x = [1, 2, 3, 4, 5, 6, 7, 8, 9];
document.write(sum(x));

</script>```

Code:
```<script type="text/javascript">
sum = function(o){
for(var s = 0, i = o.length; i; s += o[--i]);
return s;
};

var x = [1, 2, 3, 4, 5, 6, 7, 8, 9];
document.write("A = ", x, "<br />sum(A) = ", sum(x));

</script>```
Works now

• Originally Posted by howie2009
Hi Guys,
How do I sum the values of an array and output the result using document.write?

Say my array is var number=new Array(1.234,56.43,1.02);

THANKS

Thought this wud work?

Code:
```<script type="text/javascript">

var x = [1, 2, 3, 4, 5, 6, 7, 8, 9];
document.write(sum(x));

</script>```
Code:
```<script type="text/javascript">

var x = [1, 2, 3, 4, 5, 6, 7, 8, 9];
var sum = 0;
for (var i = 0; i<x.length; i++) {
sum += Number(x[i]);
}
document.write (sum);

</script>```
You should be aware that document.write() statements must be run before the page finishes loading. Any document.write() statement that runs after the page finishes loading will create a new page and overwrite all of the content of the current page (including the Javascript which called it). So document.write() is at best really only useful to write the original content of your page. It cannot be used to update the content of your page after that page has loaded. In fact document.write() is completely obsolete unless you are coding for Netscape 4.

Quizmaster: What is the meaning of the French expression "coup de grâce"?
Contestant: Lawnmower.

• Thanks Philip,

How would I amend the code to multiply the values instead of sum them?

Whats the best way to output the result instead of using document.write please?

• You should be able to do that yourself!

Code:
```<span id = "result"></span>

<script type="text/javascript">

var x = [1, 2, 3, 4, 5, 6, 7, 8, 9];
var mult = 1;
for (var i = 0; i<x.length; i++) {
mult = mult * Number(x[i]);
}
document.getElementById("result").innerHTML = mult;

</script>```
To display results use DOM methods.

• Thanks! Just getting used to javascript despite my "Regular Coder" status

Is there a way I can limit output to 2 decimals if necessary ie. 100.24 instead of 100.2425356

• Try...
Code:
`document.getElementById("result").innerHTML = mult.toFixed(2);`

• Thanks jmrker!

• [ot]

Originally Posted by Philip M
[..... So document.write() is at best really only useful to write the original content of your page.....
To write original content to your page you should be using more appropriate DOM methods.

document.write should be used only for writing content to child web pages.

[/ot]

• Thanks Guys for all the help so far!

In the below the first piece of code evaluates to 3 and the second to 7. Would it be possible to write a piece of javascript that can add the 2 results(or more results if needs be)? Something along the lines of sum1+sum2 = 10. I am trying to build a javascript spreadsheet of sorts as a pet project

Code:
```<span id = "sum1"></span>

<script type="text/javascript">
var x = [1,2];
var sum1 = 0;
for (var i = 0; i<x.length; i++) {
sum1 += Number(x[i]);
}
document.getElementById("sum1").innerHTML = sum1.toFixed(2);
</script>```
Code:
```<span id = "sum2"></span>

<script type="text/javascript">
var x = [3,4];
var sum2 = 0;
for (var i = 0; i<x.length; i++) {
sum2 += Number(x[i]);
}
document.getElementById("sum2").innerHTML = sum2.toFixed(2);
</script>```

• document.getElementById("sum1").innerHTML = sum1.toFixed(2);

You are using the same name/id for a Javascript variable and an HTML element.

To be candid, I think that you ought to be able to write this code yourself. If you cannot add up two numbers then the prognosis for your project is poor. Hint - Javascript variables declared with the var keyword within a function are local to that function (not available to other functions).

• Originally Posted by webdev1958
[ot]

To write original content to your page you should be using more appropriate DOM methods.

document.write should be used only for writing content to child web pages.

[/ot]
I did say "at best", and that to display results the OP should use DOM methods. So you post is just repetition, intended to add to your page count.

document.write() only works with pages that the browser processes as HTML. Pages the browser processes as XML cannot use document.write() at all.

However, document.write() is an easy way to demonstrate JavaScript output in a tutorial.

• Originally Posted by Philip M
I did say "at best", .....
I think we're splitting hairs here. What you said was

So document.write() is at best really only useful to write the original content of your page.
What I an saying is that document.write() is at best really only useful for writing to child pages. So I'm not sure what you're waffling on about

If someone wants to use it for other purposes that's ok

• Originally Posted by webdev1958
I think we're splitting hairs here. What you said was

What I an saying is that document.write() is at best really only useful for writing to child pages. So I'm not sure what you're waffling on about

If someone wants to use it for other purposes that's ok
Well, I am certainly not sure what you are waffling on about. Looking to start an argument as usual, I expect.

document.write is obsolete (since Netscape 4) and should be avoided except as an easy way to demonstrate JavaScript output in a tutorial.

• Originally Posted by Philip M

document.write is obsolete (since Netscape 4) and should be avoided except as an easy way to demonstrate JavaScript output in a tutorial.
I disagree. I don't have any probs using it to write to child pages. For me, that's what it's meant for and I'm going to continue using it for that and advise others to do the same where appropriate

•
Page 1 of 2 12 Last