function RatingStar(fiveStar, fourStar, threeStar, twoStar, oneStar) {
this.fiveStar = fiveStar;
this.fourStar = fourStar;
this.threeStar = threeStar;
this.twoStar = twoStar;
this.oneStar = oneStar;
this.rating = 0;
}
RatingStar.prototype.setRating = function() {
const sumFiveStar = this.fiveStar.reduce((a, c) => a + c, 0)
const sumFourStar = this.fourStar.reduce((a, c) => a + c, 0)
const sumThreeStar = this.threeStar.reduce((a, c) => a + c, 0)
const sumTwoStar = this.twoStar.reduce((a, c) => a + c, 0)
const sumOneStar = this.oneStar.reduce((a, c) => a + c, 0)
const countFiveStart = this.fiveStar.length + 1
const countFourStart = this.fourStar.length + 1
const countThreeStart = this.threeStar.length + 1
const countTwoStart = this.twoStar.length + 1
const countOneStart = this.oneStar.length + 1
const multiplyFiveStar = Math.floor(sumFiveStar) * 5
const multiplyFourStar = Math.floor(sumFourStar) * 4
const multiplyThreeStar = Math.floor(sumThreeStar) * 3
const multiplyTwoStar = Math.floor(sumTwoStar) * 2
const multiplyOneStar = Math.floor(sumOneStar) * 1
const ratingStarBeforeSum = Math.floor(sumFiveStar + sumFourStar + sumThreeStar + sumTwoStar + sumOneStar)
const ratingStarAfterSum = (multiplyFiveStar + multiplyFourStar + multiplyThreeStar + multiplyTwoStar + multiplyOneStar)
const totalPeopleGiveRating = (countFiveStart + countFourStart + countThreeStart + countTwoStart + countOneStart)
const fiveStarPersen = `${Math.floor((multiplyFiveStar / 100))}%`
const fourStarPersen = `${Math.floor((multiplyFourStar / 100))}%`
const threeStarPersen = `${Math.floor((multiplyThreeStar / 100))}%`
const twoStarPersen = `${Math.floor((multiplyTwoStar / 100))}%`
const oneStarPersen = `${Math.floor((multiplyOneStar / 100))}%`
const totalRating = +parseFloat((ratingStarAfterSum / ratingStarBeforeSum)).toFixed(1)
return {
fiveStar: {
persen: fiveStarPersen,
total: multiplyFiveStar,
peopleGiveRating: countFiveStart
},
fourStar: {
persen: fourStarPersen,
total: multiplyFourStar,
peopleGiveRating: countFourStart
},
threeStar : {
persen: threeStarPersen,
total: multiplyThreeStar,
peopleGiveRating: countThreeStart
},
twoStar: {
persen: twoStarPersen,
total: multiplyTwoStar,
peopleGiveRating: countTwoStart
},
oneStar: {
persen: twoStarPersen,
total: multiplyOneStar,
peopleGiveRating: countTwoStart
},
totalPeopleGiveRating,
totalRating
}
}
RatingStar.prototype.getRating = function() {
const data = this.setRating()
if(data.totalRating > 0 && data.totalRating == 1 && data.totalRating < 2) {
this.rating = 1
} else if(data.totalRating > 1 && data.totalRating == 2 && data.totalRating < 3) {
this.rating = 2
} else if(data.totalRating > 2 && data.totalRating == 3 && data.totalRating < 4) {
this.rating = 3
} else if(data.totalRating > 3 && data.totalRating == 4 && data.totalRating < 5) {
this.rating = 4
} else if(data.totalRating > 4 && data.totalRating == 5 && data.totalRating >= 5) {
this.rating = 5
} else if(data.totalRating > 0 && data.totalRating < 1) {
this.rating = 1.5
} else if(data.totalRating > 1 && data.totalRating < 2) {
this.rating = 2.5
} else if(data.totalRating > 2 && data.totalRating < 3) {
this.rating = 3.5
} else if(data.totalRating > 3 && data.totalRating < 4) {
this.rating = 4.5
} else if(data.totalRating > 4 && data.totalRating < 5) {
this.rating = 5.5
} else {
rating = 'tidak ada rating'
}
return Object.assign(data, {ratingStar: this.rating})
}
const fiveStar = [5, 5, 5, 5, 5, 5 , 5, 5]
const fourStar = [4, 4.5 ,4, 4, 4.5, 4.5, 4, 4.5, 4.5, 4.5, 4, 4.5, 4]
const threeStar = [3, 3, 3.5]
const twoStar = [2, 2, 2, 2.5, 2.5, 2, 2, 2.5, 2.5]
const oneStar = [1.5, 1.5 ,1, 1.5, 1.5, 1, 1, 1, 1, 1.5]
const data = new RatingStar(fiveStar, fourStar, threeStar, twoStar, oneStar)
console.log(data.getRating())