rating javascript

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() {

   // calculate rating
   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)

   // get count people give rating star
   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

   // mutiply rating with diference rating star
   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

   // sum rating
   const ratingStarBeforeSum = Math.floor(sumFiveStar + sumFourStar + sumThreeStar + sumTwoStar + sumOneStar)
   const ratingStarAfterSum = (multiplyFiveStar + multiplyFourStar + multiplyThreeStar + multiplyTwoStar + multiplyOneStar)
   const totalPeopleGiveRating = (countFiveStart + countFourStart + countThreeStart + countTwoStart + countOneStart)

   // final rating
   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})	
}

// example dummy data
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())

// output
// {
//   fiveStar: { persen: '2%', total: 200, peopleGiveRating: 9 },
//   fourStar: { persen: '2%', total: 220, peopleGiveRating: 14 },
//   threeStar: { persen: '0%', total: 27, peopleGiveRating: 4 },
//   twoStar: { persen: '0%', total: 40, peopleGiveRating: 10 },
//   oneStar: { persen: '0%', total: 12, peopleGiveRating: 10 },
//   totalPeopleGiveRating: 48,
//   totalRating: 3.6,
//   ratingStar: 4.5
// }

Are there any code examples left?
Made with love
This website uses cookies to make IQCode work for you. By using this site, you agree to our cookie policy

Welcome Back!

Sign up to unlock all of IQCode features:
  • Test your skills and track progress
  • Engage in comprehensive interactive courses
  • Commit to daily skill-enhancing challenges
  • Solve practical, real-world issues
  • Share your insights and learnings
Create an account
Sign in
Recover lost password
Or log in with

Create a Free Account

Sign up to unlock all of IQCode features:
  • Test your skills and track progress
  • Engage in comprehensive interactive courses
  • Commit to daily skill-enhancing challenges
  • Solve practical, real-world issues
  • Share your insights and learnings
Create an account
Sign up
Or sign up with
By signing up, you agree to the Terms and Conditions and Privacy Policy. You also agree to receive product-related marketing emails from IQCode, which you can unsubscribe from at any time.
Creating a new code example
Code snippet title
Source