I have a rating number for 3 different products,
and i am trying to render a star svg base of their rating number.
for example product-A has rate 3, I want to render star img, 3 times.
the maximum star they can have is 5.
i wrote a code like this
const ratingHandler = (productRate) => {
for (let i = 0; i < productRate + 1; i++) {
console.log(productRate);
return `<img src="assets/star.svg" alt="rating-star" />`;
}
};
product rate shows the rating number correctly in my console.log, but the render in the loop doesn’t render base of that number
4
Answers
Your function ends as soon as it first hits the
return
. You need to build up the complete string before returning:For fun: maybe an alternative (no loop) way of inserting the stars:
Note the great comments of @VLAZ at the question:
If you want to be the cool guy in class and nobody should understand your code, but it should still do its job, this is your go-to:
Thank me later!