Average numbers


i am trying to calculate average of numbers which are comma seperated in text area, below is the code, please help to print average in text box “Average shipment cost”

function average(itemCost) 
        {
						
		var total = 0;
		var itemCost;
		for(var i = 0; i < itemCost.length; i++) {
    		total += itemCost[i];
					}
		var avg = total / source.length;
			alert(avg)
		document.getElementById("result").innerHTML = avg;
		}

</script>


<h3> Average Shipment Cost Calculation</h3>

<form>
  
  Shipping Item Cost <textarea type="text" rows="4" cols="50"name="source" id="itemCost"></textarea><br>
  
  <input type="button" value="calculate" id="calculate" onClick="average()">

I’ve edited your post for readability. When you enter a code block into the forum, precede it with a line of three backticks and follow it with a line of three backticks to make easier to read. See this post to find the backtick on your keyboard. The “preformatted text” tool in the editor (</>) will also add backticks around text.

markdown_Forums

Your code is incomplete.

<p>
function average(itemCost) 
        {
						
		var total = 0;
		var itemCost;
		for(var i = 0; i < itemCost.length; i++) {
    		total += itemCost[i];
					}
		var avg = total / source.length;
			alert(avg)
		document.getElementById("result").innerHTML = avg;
		}

</script>


<h3> Average Shipment Cost Calculation</h3>

<form>
  
  Shipping Item Cost <textarea type="text" rows="4" cols="50"name="source" id="itemCost"></textarea><br>
  
  <input type="button" value="calculate" id="calculate" onClick="average()">
  <br>
  Average shipment cost <input type="text" name="result" id="result"><br>
	
</form>
<p> <span id = "ship"></span> </p>
</p>

Here another way to do it only if you are in full control of what data is entered into the textarea. Eval function is tricky.

Your code is still incomplete. No opening script tag. And you have p tags wrapping the whole thing?

And your code needs to be surrounded by three backticks, the key to the left of the “1” key.

i dont understand this form, provided the screenshot still bother about tags and all :frowning:

sorry my bad, unable to understand. Below is the code

<script langauge="javascript" type="text/javascript">

function average(itemCost) 
        {
						
		var total = 0;
		var itemCost;
		for(var i = 0; i < itemCost.length; i++) {
    		total += itemCost[i];
					}
		var avg = total / source.length;
			alert(avg)
		document.getElementById("result").innerHTML = avg;
		}

</script>


<h3> Average Shipment Cost Calculation</h3>

<form>
  
  Shipping Item Cost <textarea type="text" rows="4" cols="50"name="source" id="itemCost"></textarea><br>
  
  <input type="button" value="calculate" id="calculate" onClick="average()">
  <br>
  Average shipment cost <input type="text" name="result" id="result"><br>
	
</form>
<p> <span id = "ship"></span> </p>
			
    </body>

</html>

no js node scripting please only javascript. please help

thanks i tried changes as per your response, passed alert but returning NAN, i think comma between numbers are not getting split, please help

<script langauge="javascript" type="text/javascript">

function average(itemCost) 
        {
						
		var total = 0;
		var itemCost;
		for(var i = 0; i < itemCost.length; i++) {
    		total += itemCost[i];
					}
		var avg = total / itemCost.length;
			alert(avg)
		document.getElementById("result").innerHTML = avg;
		}

</script>


<h3> Average Shipment Cost Calculation</h3>

<form>
  
  Shipping Item Cost <textarea type="text" rows="4" cols="50"name="source" id="itemCost"></textarea><br>
  
  <input type="button" value="calculate" id="calculate" onClick="average(itemCost)">
  <br>
  Average shipment cost <input type="text" name="result" id="result"><br>
	
</form>
<p> <span id = "ship"></span> </p>
			
    </body>

</html>

espounding further @camperextraordinaire’s comments, your average script could look something like the below, the averaging part works fine, it’s your interaction with the DOM that is causing you problems see comments for explanation

function average(itemCost) 
        {
    itemCost = itemCost.value //item cost is still an HTML element so you must first get the value of the element
                       .split(",") // convert text into array of costs
                       .map((i)=>Number(i)) // elements of array are still strings, must convert to numbers
		var total = 0;
		for(var i = 0; i < itemCost.length; i++) {
    		total += itemCost[i];
					}
		var avg = total / itemCost.length;
			alert(avg)
		document.getElementById("result").value = avg; //change  to value as it is a text area
		}