-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathassignDataOfUser.js
42 lines (35 loc) · 1.48 KB
/
assignDataOfUser.js
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
// displayProduct.js
export function displayProduct(product) {
const img = document.getElementById('image');
const title = document.getElementById('title');
const descrip = document.getElementById('description');
const rate = document.getElementById('rating');
const price = document.getElementById('price');
const imgsClass = document.querySelector('.product-imgs');
img.src = product.img;
title.innerHTML = product.title;
descrip.innerHTML = product.description;
price.innerHTML = `$${product.price}`;
// Fetch rating and convert to stars
const starRating = convertRatingToStars(product.rating);
rate.innerHTML = starRating;
product.images.forEach((ele, idx) => {
const createImg = document.createElement('img');
createImg.src = ele;
imgsClass.appendChild(createImg); // Use appendChild (note the capitalization)
createImg.addEventListener('click',() => {
img.src=createImg.src;
img.style.width="300px";
img.style.height="300px";
})
});
}
// Function to convert rating to stars
function convertRatingToStars(rating) {
const fullStars = Math.floor(rating);
// 4.5, rating % 1 would be 0.5.
const halfStar = rating % 1 >= 0.5 ? 1 : 0;
// 4.5, full = 4, half = 1 because 0.5 = 0.5 return 1
const emptyStars = 5 - fullStars - halfStar;
return '★'.repeat(fullStars) + '☆'.repeat(halfStar) + '☆'.repeat(emptyStars);
}