我有一串用逗號分隔的數字作爲字符串。我想在JavaScript中將數字轉換爲數組,然後使用數組中的數字來執行添加和查找平均值等功能。JavaScript:將CSV轉換爲數組並對其執行操作
我的頁面是這樣的:
而且我當前的代碼是:
<script>
var number = document.getElementById("divout")
//up here i want to create an array and use the array in the function below.
var sum = 0;
var average = 0;
var squared = 0;
function math(x)
{
document.write("amount of numbers=" + x.length + "<br>")
for (var i = 0; i <= x.length - 1; i++)
{
sum = sum+a[i];
squared = squared+(x[i]*x[i]);
average = sum/x.length;
}
}
math(number)
document.write("sum="+ sum + "<br>")
document.write("average-" + average + "<br>")
document.write("squared=" + squared + "<br>")
</script>
All Divisors:
<br>
<textarea rows="10" cols="50" placeholder="Divisors" id="divout">0,1,3,7,21</textarea>
<br>
(Source)
我該怎麼辦呢?
[你需要你的代碼(不是圖像)添加到這個問題。(https://stackoverflow.com/help/mcve) – Andy
你也想加入預期的結果因爲不清楚你需要展示什麼。 – Andy
注意:[爲什麼jQuery或諸如getElementById之類的DOM方法未找到該元素?](http:// stackoverflow。com/questions/14028959/why-does-jquery-or-a-dom-method-such-getelementbyid-not-find-the-element) –