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.
Page 1 of 2 12 LastLast
Results 1 to 15 of 21
  1. #1
    Regular Coder howie2009's Avatar
    Join Date
    May 2009
    Location
    Ireland
    Posts
    206
    Thanks
    37
    Thanked 0 Times in 0 Posts

    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>
    Last edited by howie2009; 03-16-2012 at 09:09 PM.

  • #2
    Regular Coder howie2009's Avatar
    Join Date
    May 2009
    Location
    Ireland
    Posts
    206
    Thanks
    37
    Thanked 0 Times in 0 Posts
    apologies my bad

    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

  • #3
    Supreme Master coder! Philip M's Avatar
    Join Date
    Jun 2002
    Location
    London, England
    Posts
    18,310
    Thanks
    203
    Thanked 2,563 Times in 2,541 Posts
    Quote Originally Posted by howie2009 View Post
    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.

    All the code given in this post has been tested and is intended to address the question asked.
    Unless stated otherwise it is not just a demonstration.

  • #4
    Regular Coder howie2009's Avatar
    Join Date
    May 2009
    Location
    Ireland
    Posts
    206
    Thanks
    37
    Thanked 0 Times in 0 Posts
    Thanks Philip,

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

    i.e. 1x2x3x4x5 instead of 1+2+3..

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

  • #5
    Supreme Master coder! Philip M's Avatar
    Join Date
    Jun 2002
    Location
    London, England
    Posts
    18,310
    Thanks
    203
    Thanked 2,563 Times in 2,541 Posts
    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.

    All the code given in this post has been tested and is intended to address the question asked.
    Unless stated otherwise it is not just a demonstration.

  • #6
    Regular Coder howie2009's Avatar
    Join Date
    May 2009
    Location
    Ireland
    Posts
    206
    Thanks
    37
    Thanked 0 Times in 0 Posts
    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
    Last edited by howie2009; 03-16-2012 at 10:00 PM.

  • #7
    Senior Coder jmrker's Avatar
    Join Date
    Aug 2006
    Location
    FL
    Posts
    3,146
    Thanks
    39
    Thanked 505 Times in 499 Posts
    Try...
    Code:
    document.getElementById("result").innerHTML = mult.toFixed(2);

  • #8
    Regular Coder howie2009's Avatar
    Join Date
    May 2009
    Location
    Ireland
    Posts
    206
    Thanks
    37
    Thanked 0 Times in 0 Posts
    Thanks jmrker!

  • #9
    Banned
    Join Date
    Apr 2011
    Posts
    656
    Thanks
    14
    Thanked 69 Times in 69 Posts
    [ot]

    Quote Originally Posted by Philip M View Post
    [..... 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]

  • #10
    Regular Coder howie2009's Avatar
    Join Date
    May 2009
    Location
    Ireland
    Posts
    206
    Thanks
    37
    Thanked 0 Times in 0 Posts
    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>

  • #11
    Supreme Master coder! Philip M's Avatar
    Join Date
    Jun 2002
    Location
    London, England
    Posts
    18,310
    Thanks
    203
    Thanked 2,563 Times in 2,541 Posts
    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).

    All the code given in this post has been tested and is intended to address the question asked.
    Unless stated otherwise it is not just a demonstration.

  • #12
    Supreme Master coder! Philip M's Avatar
    Join Date
    Jun 2002
    Location
    London, England
    Posts
    18,310
    Thanks
    203
    Thanked 2,563 Times in 2,541 Posts
    Quote Originally Posted by webdev1958 View Post
    [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.

    All the code given in this post has been tested and is intended to address the question asked.
    Unless stated otherwise it is not just a demonstration.

  • #13
    Banned
    Join Date
    Apr 2011
    Posts
    656
    Thanks
    14
    Thanked 69 Times in 69 Posts
    Quote Originally Posted by Philip M View Post
    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

  • #14
    Supreme Master coder! Philip M's Avatar
    Join Date
    Jun 2002
    Location
    London, England
    Posts
    18,310
    Thanks
    203
    Thanked 2,563 Times in 2,541 Posts
    Quote Originally Posted by webdev1958 View Post
    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.

    All the code given in this post has been tested and is intended to address the question asked.
    Unless stated otherwise it is not just a demonstration.

  • #15
    Banned
    Join Date
    Apr 2011
    Posts
    656
    Thanks
    14
    Thanked 69 Times in 69 Posts
    Quote Originally Posted by Philip M View Post

    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 LastLast

    Posting Permissions

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