rating javascript

Solutions on MaxInterview for rating javascript by the best coders in the world

showing results for - "rating javascript"
Lena
04 Feb 2017
1function RatingStar(fiveStar, fourStar, threeStar, twoStar, oneStar)  {
2	this.fiveStar = fiveStar;
3	this.fourStar = fourStar;
4	this.threeStar = threeStar;
5	this.twoStar = twoStar;
6	this.oneStar = oneStar;
7	this.rating = 0;
8}
9
10RatingStar.prototype.setRating = function() {
11
12   // calculate rating
13   const sumFiveStar = this.fiveStar.reduce((a, c) => a + c, 0)
14   const sumFourStar = this.fourStar.reduce((a, c) => a + c, 0)
15   const sumThreeStar = this.threeStar.reduce((a, c) => a + c, 0)
16   const sumTwoStar = this.twoStar.reduce((a, c) => a + c, 0)
17   const sumOneStar = this.oneStar.reduce((a, c) => a + c, 0)
18
19   // get count people give rating star
20   const countFiveStart = this.fiveStar.length + 1
21   const countFourStart = this.fourStar.length + 1
22   const countThreeStart = this.threeStar.length + 1
23   const countTwoStart = this.twoStar.length + 1
24   const countOneStart = this.oneStar.length + 1
25
26   // mutiply rating with diference rating star
27   const multiplyFiveStar = Math.floor(sumFiveStar) * 5
28   const multiplyFourStar = Math.floor(sumFourStar) * 4
29   const multiplyThreeStar = Math.floor(sumThreeStar) * 3
30   const multiplyTwoStar = Math.floor(sumTwoStar) * 2
31   const multiplyOneStar = Math.floor(sumOneStar) * 1
32
33   // sum rating
34   const ratingStarBeforeSum = Math.floor(sumFiveStar + sumFourStar + sumThreeStar + sumTwoStar + sumOneStar)
35   const ratingStarAfterSum = (multiplyFiveStar + multiplyFourStar + multiplyThreeStar + multiplyTwoStar + multiplyOneStar)
36   const totalPeopleGiveRating = (countFiveStart + countFourStart + countThreeStart + countTwoStart + countOneStart)
37
38   // final rating
39   const fiveStarPersen = `${Math.floor((multiplyFiveStar / 100))}%`
40   const fourStarPersen = `${Math.floor((multiplyFourStar / 100))}%`
41   const threeStarPersen = `${Math.floor((multiplyThreeStar / 100))}%`
42   const twoStarPersen = `${Math.floor((multiplyTwoStar / 100))}%`
43   const oneStarPersen = `${Math.floor((multiplyOneStar / 100))}%`
44
45   const totalRating = +parseFloat((ratingStarAfterSum / ratingStarBeforeSum)).toFixed(1)
46   return {
47   	fiveStar: {
48   	  persen: fiveStarPersen,
49   	  total: multiplyFiveStar,
50   	  peopleGiveRating: countFiveStart
51   	},
52   	fourStar: {
53   	  persen: fourStarPersen,
54   	  total: multiplyFourStar,
55   	  peopleGiveRating: countFourStart
56   	},
57   	threeStar : {
58   	  persen: threeStarPersen,
59   	  total: multiplyThreeStar,
60   	  peopleGiveRating: countThreeStart
61   	},
62   	twoStar: {
63   	  persen: twoStarPersen,
64   	  total: multiplyTwoStar,
65   	  peopleGiveRating: countTwoStart
66   	},
67   	oneStar: {
68   	  persen: twoStarPersen,
69   	  total: multiplyOneStar,
70   	  peopleGiveRating: countTwoStart
71   	},
72   	totalPeopleGiveRating,
73   	totalRating
74   }
75}
76
77RatingStar.prototype.getRating = function() {
78
79	const data = this.setRating()
80
81	if(data.totalRating > 0 && data.totalRating == 1 && data.totalRating < 2) {
82		this.rating = 1
83
84	} else if(data.totalRating > 1 && data.totalRating == 2 && data.totalRating < 3) {
85		this.rating = 2
86
87	} else if(data.totalRating > 2 && data.totalRating == 3 && data.totalRating < 4) {
88		this.rating = 3
89
90	} else if(data.totalRating > 3 && data.totalRating == 4 && data.totalRating < 5) {
91		this.rating = 4
92
93	} else if(data.totalRating > 4 && data.totalRating == 5 && data.totalRating >= 5) {
94		this.rating = 5
95
96	} else if(data.totalRating > 0 && data.totalRating < 1) {
97		this.rating = 1.5
98
99	} else if(data.totalRating > 1 && data.totalRating < 2) {
100		this.rating = 2.5
101
102	} else if(data.totalRating > 2 && data.totalRating < 3) {
103		this.rating = 3.5
104
105	} else if(data.totalRating > 3 && data.totalRating < 4) {
106		this.rating = 4.5
107
108	} else if(data.totalRating > 4 && data.totalRating < 5) {
109		this.rating = 5.5
110
111	} else {
112		rating = 'tidak ada rating'
113	}
114
115	return Object.assign(data, {ratingStar: this.rating})	
116}
117
118// example dummy data
119const fiveStar = [5, 5, 5, 5, 5, 5 , 5, 5] 
120const fourStar = [4, 4.5 ,4, 4, 4.5, 4.5, 4, 4.5, 4.5, 4.5, 4, 4.5, 4]  
121const threeStar = [3, 3, 3.5]
122const twoStar = [2, 2, 2, 2.5, 2.5, 2, 2, 2.5, 2.5] 
123const oneStar = [1.5, 1.5 ,1, 1.5, 1.5, 1, 1, 1, 1, 1.5] 
124
125const data = new RatingStar(fiveStar, fourStar, threeStar, twoStar, oneStar)
126console.log(data.getRating())
127
128// output
129// {
130//   fiveStar: { persen: '2%', total: 200, peopleGiveRating: 9 },
131//   fourStar: { persen: '2%', total: 220, peopleGiveRating: 14 },
132//   threeStar: { persen: '0%', total: 27, peopleGiveRating: 4 },
133//   twoStar: { persen: '0%', total: 40, peopleGiveRating: 10 },
134//   oneStar: { persen: '0%', total: 12, peopleGiveRating: 10 },
135//   totalPeopleGiveRating: 48,
136//   totalRating: 3.6,
137//   ratingStar: 4.5
138// }