Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6477711
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 25, 20262026-05-25T06:58:54+00:00 2026-05-25T06:58:54+00:00

What I want to do is have numbers inputted by user and the sum

  • 0

What I want to do is have numbers inputted by user and the sum of the numbers returned. My logic is as follows:

  1. User inputs string
  2. String is split to array
  3. Loop through array and sum all numbers
  4. Return sum

And here is the code I have so far:

<script type='text/javascript'>

var val=document.getElementById('userInput').value;
var temp=val.split(" ");

function sum() {
    for(var i=0, MISSING THIS BIT

    document.getElementById('resultSum').innerHTML=MISSING THIS BIT;
}

</script>

<form name="input">
    <textarea name="userInput" rows=20 cols=20></textarea>
    <input name="Run" type=Button value="run" onClick="sum()">
<form name="resultSum"><input type=Text>

I admit with humility that this is mostly probably wrong and appreciate anybody’s time and effort.


UPDATE: I have done as suggested and I get the following error on my code below:

Message: ‘document.getElementById(…)’ is null or not an object Line:
16 Char: 1 Code: 0

<html>

<script type='text/javascript'>

function sum(){
    var val = document.getElementById('userInput').value;
    var temp = val.split(" ");

    var total = 0;
    var v;
    for(var i = 0; i < temp.length; i++) {
        v = parseFloat(temp[i]);
        if (!isNaN(v)) total += v;
    }

    document.getElementById('resultSum').innerHTML=total;

}

</script>

<form name="input">
    <textarea name="userInput" rows=20 cols=20></textarea>
    <input name="Run" type=Button value="run" onClick="sum()">
    <form name="resultSum"><input type=text>
<html>

Any suggestions? Thanks to all for being comprehensive – I have read both examples and understand the process now!

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-25T06:58:55+00:00Added an answer on May 25, 2026 at 6:58 am

    You want a basic loop to convert and add each item.

    I have also cleaned up your HTML a ton. You didn’t have any proper closing tags. I have also changed all of the ‘name’ attributes to ‘id’ attributes so that ‘getElementById’ would work properly, which I missed on my first pass.

    <html>
      <head>
        <script type='text/javascript'>
          function sum(){ 
            var val = document.getElementById('userInput').value;
            var temp = val.split(" ");
            var total = 0;
            var v;
            for(var i = 0; i < temp.length; i++) {
              v = parseFloat(temp[i]);
              if (!isNaN(v)) total += v; 
            } 
            document.getElementById('resultSumValue').value = total; 
          } 
        </script>
      </head>
      <body>
        <form id="input">
          <textarea id="userInput" rows=20 cols=20></textarea> 
          <input id="Run" type=Button value="run" onClick="sum()" />
        </form>
    
        <form id="resultSum">
          <input id="resultSumValue" type="text" />
        </form>
      </body>
    </html>
    

    This will also ignore any values that are ‘NaN’ (Not a Number).

    If you want the numbers to only be integers (no decimals), change parseFloat to parseInt.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a string (char) and I want to extract numbers out of it.
In a table column in string we can have numbers/special chars/white spaces. I want
I have a list of numbers that are inputted by users. I want to
i have two numbers 1,2 in one array.Now i want to generate all the
I have numbers in javascript from 01(int) to 09(int) and I want add 1(int)
I want to have a button that has numbers in the range 0 ...
I have signed numbers (2s complement) stored in 32-bit integers, and I want to
I have some numbers stored in a std::vector<int> . I want to find which
I have 1M numbers:N[], and 1 single number n, now I want to find
I have a bunch of numbers timestamps that I want to check against a

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.