11const products = [
2- {
2+ {
33 id : 1 ,
44 name : "Wireless Headphones" ,
55 category : "Electronics" ,
@@ -133,24 +133,62 @@ const products = [
133133
134134const mainDiv = document . getElementById ( "main" ) ;
135135
136- // id: 1,
137- // name: "Wireless Bluetooth Headphones",
138- // category: "Electronics",
139- // brand: "Sony",
140- // actualPrice: 5999,
141- // discountedPrice: 4499,
142- // ratings: [5, 4, 4, 5, 3],
143- // quantity: 120,
144- // description: "Noise-cancelling over-ear headphones with 30 hours battery life.",
145- // inStock: true,
146- // image: "https://picsum.photos/id/1011/400/400"
147- const productCard = products . map ( ( product ) =>
136+ // id: 1,
137+ // name: "Wireless Bluetooth Headphones",
138+ // category: "Electronics",
139+ // brand: "Sony",
140+ // actualPrice: 5999,
141+ // discountedPrice: 4499,
142+ // ratings: [5, 4, 4, 5, 3],
143+ // quantity: 120,
144+ // description: "Noise-cancelling over-ear headphones with 30 hours battery life.",
145+ // inStock: true,
146+ // image: "https://picsum.photos/id/1011/400/400"
147+ // const averageRatingsofEachProduct = document.getElementById("averageRatingsofEachProduct");
148+
149+
150+
151+ const categoryNav = document . getElementById ( 'categoryNav' ) ;
152+ const ul = document . createElement ( 'ul' ) ;
153+
154+ const uniqueCategories = [ ] ;
155+
156+ for ( const p of products ) {
157+ if ( ! uniqueCategories . includes ( p . category ) ) {
158+ uniqueCategories . push ( p . category )
159+ }
160+ }
161+
162+ const navTabs = uniqueCategories . forEach ( category => {
163+ const li = document . createElement ( 'li' ) ;
164+ li . innerText = category ;
165+ ul . appendChild ( li ) ;
166+ } )
167+
168+ categoryNav . append ( ul ) ;
169+
170+
171+
172+
173+ products . forEach ( p => {
174+ const avg = p . ratings . reduce ( ( a , b ) => a + b , 0 ) / p . ratings . length ;
175+
176+ // const rating = document.createElement('div');
177+ // rating.innerHTML = `<p>${p.name} - ${avg.toFixed(1)}</p>`;
178+
179+ // averageRatingsofEachProduct.append(rating);
180+
181+ p . avg = avg . toFixed ( 1 ) ;
182+ } ) ;
183+
184+ const productCard = products . map ( ( product ) =>
148185 `
149186 <div class="card">
150187 <img src=${ product . image } alt=${ product . name } />
151188 <h1>${ product . name } </h1>
152189 <p>${ product . description } </p>
153190 <p>${ product . discountedPrice } - <strike>${ product . actualPrice } </strike></p>
191+ Average rating - <h5 style="display:inline">${ product . avg } </h5>
154192 <button>Add to cart</button>
155193 </div>
156194 `
@@ -160,45 +198,47 @@ mainDiv.innerHTML = productCard;
160198
161199const submitBtn = document . getElementById ( 'filterBtn' ) ;
162200
163- submitBtn . addEventListener ( 'click' , ( ) => {
201+ submitBtn . addEventListener ( 'click' , ( ) => {
164202 const startPrice = document . getElementById ( 'start' ) . value ;
165203 const endPrice = document . getElementById ( 'end' ) . value ;
166204
167- const filteredProducts = products . filter ( ( product ) => product . actualPrice >= startPrice && product . actualPrice <= endPrice ) ;
205+ const filteredProducts = products . filter ( ( product ) => product . actualPrice >= startPrice && product . actualPrice <= endPrice ) ;
168206
169- if ( filteredProducts . length == 0 ) {
207+ if ( filteredProducts . length == 0 ) {
170208 mainDiv . innerHTML = "No results found"
171209 }
172- else {
173- const filteredData = filteredProducts . map ( ( product ) =>
174- `
210+ else {
211+ const filteredData = filteredProducts . map ( ( product ) =>
212+ `
175213 <div class="card">
176214 <img src=${ product . image } alt=${ product . name } />
177215 <h1>${ product . name } </h1>
178216 <p>${ product . description } </p>
179217 <p>${ product . discountedPrice } - <strike>${ product . actualPrice } </strike></p>
218+ Average rating - <h5 style="display:inline">${ product . avg } </h5>
180219 <button>Add to cart</button>
181220 </div>
182221
183222 `
184- ) . join ( " " )
185- mainDiv . innerHTML = filteredData ;
186- }
223+ ) . join ( " " )
224+ mainDiv . innerHTML = filteredData ;
225+ }
187226} )
188227
189228
190229const sortBtn = document . getElementById ( 'sort' )
191230
192- sortBtn . addEventListener ( 'click' , ( ) => {
193- const sortedproducts = products . sort ( ( a , b ) => a . discountedPrice - b . discountedPrice ) ;
231+ sortBtn . addEventListener ( 'click' , ( ) => {
232+ const sortedproducts = products . sort ( ( a , b ) => a . discountedPrice - b . discountedPrice ) ;
194233
195- const sortedData = sortedproducts . map ( ( product ) =>
234+ const sortedData = sortedproducts . map ( ( product ) =>
196235 `
197236 <div class="card">
198237 <img src=${ product . image } alt=${ product . name } />
199238 <h1>${ product . name } </h1>
200239 <p>${ product . description } </p>
201240 <p>${ product . discountedPrice } - <strike>${ product . actualPrice } </strike></p>
241+ Average rating - <h5 style="display:inline">${ product . avg } </h5>
202242 <button>Add to cart</button>
203243 </div>
204244 `
@@ -209,4 +249,3 @@ sortBtn.addEventListener('click',()=>{
209249
210250
211251
212-
0 commit comments