Loop array and return sum of all values

What I want to do is the numbers entered by the user and the sum of the numbers returned. My logic is this:

  • User enters a string
  • The string is split into an array
  • Scroll the array and sum all the numbers
  • Refundable amount

And here is the code that I still have:

<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 sympathy that this is most likely wrong, and appreciate any time and effort.


UPDATE: I did this as a suggestion, and I got the following error in my code below:

Message: 'document.getElementById (...)' is a null or not object. String: 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 everyone for being comprehensive - I read both examples and understood the process now!

+4
source share
4 answers

You need a basic loop to convert and add each element.

I also cleaned your HTML ton. You did not have any matching end tags. I also changed all the "name" attributes to the "id" attributes so that "getElementById" worked correctly, which I missed in 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> 

It also ignores any values ​​that are "NaN" (Not a Number).

If you only need integers (no decimals), replace parseFloat with parseInt.

+8
source

Here is the outline, but I think it's worth writing it yourself to find out.

+2
source

Start by declaring the set variable 0 before the for loop. Let's move on to each element of the array (array.length), adding to the variable that you set before the for loop.

0
source
 <html> <head> <title>sum input</title> <style> p, textarea,input{font-size:24px} textarea,input{text-align:right} </style> <script> function sum(){ var val= document.getElementById('userInput').value.replace(/^\D+|D+$/g,''); document.getElementById('resultSum').value= eval(val.replace(/\D+/g,'+')); } </script> </head> <body> <p>Enter a series of numbers :<br> <textarea id="userInput" rows="10" cols="10"></textarea><br> <input id="Run" type="button" value="Sum" onClick="sum()"> <input id="resultSum" type="text" readOnly> </p> </body> </html> 
0
source

All Articles