Skip to content

Instantly share code, notes, and snippets.

Created March 3, 2016 20:54
Show Gist options
  • Save anonymous/9443952082bc70cf7095 to your computer and use it in GitHub Desktop.
Save anonymous/9443952082bc70cf7095 to your computer and use it in GitHub Desktop.
Services average // source http://jsbin.com/dekogepivu
<!DOCTYPE html>
<html>
<head>
<meta charset="ISO-8859-1">
<title>Services average</title>
</head>
<body>
<select id="service">
<option value="none">Select Service</option>
<option value="carpentry">Carpentry</option>
<option value="fencing">Fencing</option>
<option value="doors">Doors</option>
<option value="windows">Windows</option>
<option value="drywall">Drywall</option>
<option value="electrical">Electrical</option>
</select>
<h4 id="avg"></h4>
<script>
var services = {
carpentry: [ 20.33, 30.43, 20.90 ],
fencing: [ 45.66, 87.56, 34.55 ],
doors: [ 66.66, 36.75, 23.57 ],
windows: [ 56.32, 78.89, 34.67 ],
drywall: [ 84.74, 43.46, 45.24 ],
electrical: [ 46.35, 90.34, 34.35 ]
}
document.getElementById("service").addEventListener("change", function(e){
if(e.target.value === "none") return document.getElementById("avg").innerHTML = "";
var sum = services[e.target.value].reduce(function(a,b){ return a+b })
var avg = Math.round(sum/services[e.target.value].length * 100)/100;
document.getElementById("avg").innerHTML = " The average price for " +e.target.value+ " is: " + avg;
});
</script>
</body>
</html>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment