-
Notifications
You must be signed in to change notification settings - Fork 10
/
ratings.html
64 lines (56 loc) · 2.18 KB
/
ratings.html
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>Rating Calculator</title>
<style>
</style>
</head>
<body>
# of 5 Star: <input id="five-star" type="number"><br>
# of 4 Star: <input id="four-star" type="number"><br>
# of 3 Star: <input id="three-star" type="number"><br>
# of 2 Star: <input id="two-star" type="number"><br>
# of 1 Star: <input id="one-star" type="number"><br><br>
Number of ratings: <span id="number-ratings"></span><br>
Average Rating: <span id="average-rating"></span>
</body>
<script>
window.onload = function() {
var array = ["five-star", "four-star", "three-star", "two-star", "one-star"]
for (var i = 0; i < array.length; i++) {
document.getElementById(array[i]).addEventListener('input', function(e) {
computeTotal();
computeAverage();
}, false);
}
}
function computeAverage() {
var five = document.getElementById("five-star")
var four = document.getElementById("four-star")
var three = document.getElementById("three-star")
var two = document.getElementById("two-star")
var one = document.getElementById("one-star")
var total = number(five.value) + number(four.value) + number(three.value) + number(two.value) + number(one.value)
var weighted = number(five.value) * 5 + number(four.value) * 4 + number(three.value) * 3 + number(two.value) * 2 + number(one.value) * 1
document.getElementById("average-rating").innerHTML = weighted / total
}
function computeTotal() {
var five = document.getElementById("five-star")
var four = document.getElementById("four-star")
var three = document.getElementById("three-star")
var two = document.getElementById("two-star")
var one = document.getElementById("one-star")
var total = number(five.value) + number(four.value) + number(three.value) + number(two.value) + number(one.value)
document.getElementById("number-ratings").innerHTML = total
}
function number(value) {
var number = parseInt(value)
if (number !== number) {
return 0 // NaN
} else {
return number
}
}
</script>
</html>