-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.html
142 lines (138 loc) · 5.2 KB
/
index.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
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<link rel="stylesheet" href="bootstrap.min.css">
<link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/6.2.0/css/all.min.css" integrity="sha512-xh6O/CkQoPOWDdYTDqeRdPCVd1SpvCA9XXcUnZS2FmJNp1coAFzvtCN9BmamE+4aHK8yyUHUSCcJHgXloTyT2A==" crossorigin="anonymous" referrerpolicy="no-referrer" />
<link rel="stylesheet" href="style.css">
<title>Star Rating</title>
</head>
<body>
<div class="container">
<div class="form-group">
<select name="" id="product-select" class="mt-4 form-control
custom-select">
<option value="0" disabled selected>Select Product</option>
<option value="sony">Sony 4K TV</option>
<option value="samsung">Samsung 4K TV</option>
<option value="vizio">Vizio 4K TV</option>
<option value="panasonic">Panasonic 4K TV</option>
<option value="phillips">Phillips 4K TV</option>
</select>
</div>
<div class="form-group mt-4">
<input type="number" id="rating-control"
class="form-control" step="0.1" max="5" placeholder="Rate 1-5">
</div>
<table class="table table-striped mt-4">
<thead>
<tr>
<th>4K Television</th>
<th> Rating</th>
</tr>
</thead>
<tbody>
<tr class="sony">
<td>Sony 4K TV</td>
<td>
<div class="stars-outer">
<div class="stars-inner"></div>
</div>
<span class="number-rating"></span>
</td>
</tr>
<tr class="samsung">
<td>Samsung 4K TV</td>
<td>
<div class="stars-outer">
<div class="stars-inner"></div>
</div>
<span class="number-rating"></span>
</td>
</tr>
<tr class="vizio">
<td>Vizio 4K TV</td>
<td>
<div class="stars-outer">
<div class="stars-inner"></div>
</div>
<span class="number-rating"></span>
</td>
</tr>
<tr class="panasonic">
<td>Panasonic 4K TV</td>
<td>
<div class="stars-outer">
<div class="stars-inner"></div>
</div>
<span class="number-rating"></span>
</td>
</tr>
<tr class="phillips">
<td>Phillips 4K TV</td>
<td>
<div class="stars-outer">
<div class="stars-inner"></div>
</div>
<span class="number-rating"></span>
</td>
</tr>
</tbody>
</table>
</div>
<script>
//Initial Ratings
const ratings={
sony:4.7,
samsung:3.4,
vizio:2.3,
panasonic:3.6,
phillips:4.1
}
//Total Stars
const starsTotal=5;
//Run getRatings when DOM loads
document.addEventListener('DOMContentLoaded',getRatings);
//Form Elements
const productSelect=document.getElementById('product-select');
const ratingControl=document.getElementById('rating-control');
//Init product
let product;
//Product select change
productSelect.addEventListener('change',(e)=>{
product=e.target.value;
//Enable rating control
ratingControl.disabled=false;
ratingControl.value=ratings[product];
});
//Rating control change
ratingControl.addEventListener('blur',(e)=>{
const rating=e.target.value;
//Make sure 5 or under
if(rating>5){
alert('Please rate 1-5');
return;
}
//Change rating
ratings[product]=rating;
getRatings();
});
//Get ratings
function getRatings(){
for(let rating in ratings)
{
//get percentage
const starPercentage=(ratings[rating]/starsTotal)*100;
//round to nearest 10
const starPercentageRounded=`${Math.round(starPercentage/10)*10}%`;
//set width of stars-inner to percentage
document.querySelector(`.${rating} .stars-inner`).style.width=starPercentageRounded;
//add number rating
document.querySelector(`.${rating} .number-rating`).innerHTML=ratings[rating];
}
}
</script>
</body>
</html>