Skip to content

Instantly share code, notes, and snippets.

@jonasalmeida
Created December 5, 2014 01:44
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save jonasalmeida/378d86ac58ee8a9ef967 to your computer and use it in GitHub Desktop.
Save jonasalmeida/378d86ac58ee8a9ef967 to your computer and use it in GitHub Desktop.
mad
<h2>Mean Absolute Deviation = <span id="y"> ... </span></h2>
<p><textarea id="x"></textarea></p>
(separate values with commas)
x.style.width="100%"
mean = function(x){
var s = 0, n = x.length
for(i=0;i<n;i++){s+=x[i]}
return s/n
}
mad=function(x){
var avg = mean(x)
x = x.map(function(xi){
return Math.abs(avg-xi)
})
return mean(x)
}
x.onkeyup=function(evt){
var v = x.value;
if(v.slice(-1)!=','){
v=v.split(',').map(function(vi){return parseFloat(vi)})
y.textContent=mad(v)
}
//console.log(v)
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment