This post mark down the logic using javaSrcipt execute simply mean calaulation.
source samples are with string data type and separated by comma.
using this formula :
<script>
const ROUND_TO_FIXED = 2;
function _stringToList(string){
return string.split(",");
}
function _roundToFixed(num){
return Number(parseFloat(num).toFixed(ROUND_TO_FIXED));
}
function getSampleMean(string){
var sourceList = _stringToList(string),
sum = 0;
for(var i in sourceList){
sum += parseFloat(sourceList[i]);
}
return _roundToFixed(sum / sourceList.length);
}
var source = "1,2,3,5,1,2,6"
console.log(getSampleMean(source));
</script>
Result:
2.86
Reference:
https://en.wikipedia.org/wiki/Mean
https://www.w3schools.com/Js/js_json_parse.asp
https://stackoverflow.com/questions/9907419/how-to-get-a-key-in-a-javascript-object-by-its-value
Monday, August 12, 2019
[JavaScript][Example] get simple mean from a list of numbers
分類:
JavaScript
,
Mathematics
,
Statistics
Subscribe to:
Post Comments
(
Atom
)
No comments :
Post a Comment