...

# sum of array values

howie2009
03-16-2012, 09:03 PM
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?

<script type="text/javascript">

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

</script>

howie2009
03-16-2012, 09:12 PM

<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

Philip M
03-16-2012, 09:16 PM
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?

<script type="text/javascript">

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

</script>

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

howie2009
03-16-2012, 09:41 PM
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?

Philip M
03-16-2012, 09:51 PM
You should be able to do that yourself!

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

howie2009
03-16-2012, 09:56 PM
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

jmrker
03-16-2012, 10:38 PM
Try...

document.getElementById("result").innerHTML = mult.toFixed(2);

howie2009
03-17-2012, 12:23 AM
Thanks jmrker!

webdev1958
03-17-2012, 02:11 AM
[..... 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.

howie2009
03-17-2012, 04:53 AM
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

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

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

Philip M
03-17-2012, 09:35 AM
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).

Philip M
03-17-2012, 09:46 AM
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.

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

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.

webdev1958
03-17-2012, 10:50 AM
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 :cool:

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

Philip M
03-17-2012, 11:12 AM
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 :cool:

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.

webdev1958
03-17-2012, 11:20 AM
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 :thumbsup:

Philip M
03-17-2012, 11:24 AM
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 :thumbsup:

If you want to code badly, that is up to you. :p

Among other issues, content written in with document.write() may not subsequently appear in the page’s DOM, which means no further access to manipulate it programmatically, and no access to accessibility APIs. Only the crudest code uses document.write() for any purpose except that which I mentioned.

webdev1958
03-17-2012, 11:28 AM
If you want to code badly, that is up to you. :p

Among other issues, content written in with document.write() may not subsequently appear in the page’s DOM, which means no further access to manipulate it programmatically, and no access to accessibility APIs.

I don't see using document.write() to write data to child pages as bad code.

I don't have issues with it in that context. Obviously,if it is the best option depends on the circumstances and the purpose of writing to the child page in the first place :)

Philip M
03-17-2012, 11:33 AM
I don't see using document.write() to write data to child pages as bad code.

I don't have issues with it in that context. Obviously,if it is the best option depends on the circumstances and the purpose of writing to the child page in the first place :)

Well, I do see it as bad code. And I think you would find that most people here would agree wth that. But I see that you are backing off now. ;)

webdev1958
03-17-2012, 11:36 AM
But I see that you are backing off now. ;)

I don't see how :)

Earlier I posted

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 appropriateNothing's changed :thumbsup:

howie2009
03-17-2012, 12:36 PM
Bring back the love! Give me cheap women and beer to techie disputes any day:thumbsup:

Philip M
03-17-2012, 02:35 PM
Bring back the love! Give me cheap women and beer to techie disputes any day:thumbsup:

Some people could walk into an empty room and still manage to start an argument! Best to ignore all trolls. :)