aldikhan13

Simple Ratingstar Javascript

May 29th, 2021 (edited)
992
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. function 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.  
  10. RatingStar.prototype.percentage = function(sumFiveStar, sumFourStar, sumThreeStar, sumTwoStar, sumOneStar) {
  11.    
  12.     let persensFiveStar = 0
  13.     let persenFourStar = 0
  14.     let persenThreeStar = 0
  15.     let persenTwoStar = 0
  16.     let persenOneStar = 0
  17.  
  18.     let fiveStarLength = sumFiveStar.toString().length
  19.     let fourStarLength = sumFourStar.toString().length
  20.     let threeStarLength = sumThreeStar.toString().length
  21.     let twoStarLength = sumTwoStar.toString().length
  22.     let oneStarLength = sumOneStar.toString().length
  23.  
  24.     if(
  25.         (fiveStarLength || fourStarLength || threeStarLength | twoStarLength || sumTwoStar || oneStarLength) >= 2 ||
  26.         (fiveStarLength || fourStarLength || threeStarLength | twoStarLength || sumTwoStar || oneStarLength) < 5) {
  27.  
  28.         persensFiveStar = `${Math.floor((sumFiveStar / 100))}%`
  29.         persenFourStar = `${Math.floor((sumFourStar / 100))}%`
  30.         persenThreeStar = `${Math.floor((sumThreeStar / 100))}%`
  31.         persenTwoStar = `${Math.floor((sumTwoStar / 100))}%`
  32.         persenOneStar = `${Math.floor((sumOneStar / 100))}%`
  33.  
  34.     } else if(
  35.         (fiveStarLength || fourStarLength || threeStarLength | twoStarLength || sumTwoStar || oneStarLength) >= 5 ||
  36.         (fiveStarLength || fourStarLength || threeStarLength | twoStarLength || sumTwoStar || oneStarLength) < 6) {
  37.  
  38.         persensFiveStar = `${Math.floor((sumFiveStar / 1000))}%`
  39.         persenFourStar = `${Math.floor((sumFourStar / 1000))}%`
  40.         persenThreeStar = `${Math.floor((sumThreeStar / 1000))}%`
  41.         persenTwoStar = `${Math.floor((sumTwoStar / 1000))}%`
  42.         persenOneStar = `${Math.floor((sumOneStar / 1000))}%`
  43.  
  44.     } else if(
  45.         (fiveStarLength || fourStarLength || threeStarLength | twoStarLength || sumTwoStar || oneStarLength) >= 6 ||
  46.         (fiveStarLength || fourStarLength || threeStarLength | twoStarLength || sumTwoStar || oneStarLength) < 7) {
  47.  
  48.         persensFiveStar = `${Math.floor((sumFiveStar / 10000))}%`
  49.         persenFourStar = `${Math.floor((sumFourStar / 10000))}%`
  50.         persenThreeStar = `${Math.floor((sumThreeStar / 10000))}%`
  51.         persenTwoStar = `${Math.floor((sumTwoStar / 10000))}%`
  52.         persenOneStar = `${Math.floor((sumOneStar / 10000))}%`
  53.  
  54.     } else if(
  55.         (fiveStarLength || fourStarLength || threeStarLength | twoStarLength || sumTwoStar || oneStarLength) >= 7 ||
  56.         (fiveStarLength || fourStarLength || threeStarLength | twoStarLength || sumTwoStar || oneStarLength) < 8) {
  57.  
  58.         persensFiveStar = `${Math.floor((sumFiveStar / 100000))}%`
  59.         persenFourStar = `${Math.floor((sumFourStar / 100000))}%`
  60.         persenThreeStar = `${Math.floor((sumThreeStar / 100000))}%`
  61.         persenTwoStar = `${Math.floor((sumTwoStar / 100000))}%`
  62.         persenOneStar = `${Math.floor((sumOneStar / 100000))}%`
  63.  
  64.     } else if(
  65.         (fiveStarLength || fourStarLength || threeStarLength | twoStarLength || sumTwoStar || oneStarLength) >= 8 ||
  66.         (fiveStarLength || fourStarLength || threeStarLength | twoStarLength || sumTwoStar || oneStarLength) < 9) {
  67.  
  68.         persensFiveStar = `${Math.floor((sumFiveStar / 1000000))}%`
  69.         persenFourStar = `${Math.floor((sumFourStar / 1000000))}%`
  70.         persenThreeStar = `${Math.floor((sumThreeStar / 1000000))}%`
  71.         persenTwoStar = `${Math.floor((sumTwoStar / 1000000))}%`
  72.         persenOneStar = `${Math.floor((sumOneStar / 1000000))}%`
  73.  
  74.     } else if(
  75.         (fiveStarLength || fourStarLength || threeStarLength | twoStarLength || sumTwoStar || oneStarLength) >= 9 ||
  76.         (fiveStarLength || fourStarLength || threeStarLength | twoStarLength || sumTwoStar || oneStarLength) < 10) {
  77.  
  78.         persensFiveStar = `${Math.floor((sumFiveStar / 10000000))}%`
  79.         persenFourStar = `${Math.floor((sumFourStar / 10000000))}%`
  80.         persenThreeStar = `${Math.floor((sumThreeStar / 10000000))}%`
  81.         persenTwoStar = `${Math.floor((sumTwoStar / 10000000))}%`
  82.         persenOneStar = `${Math.floor((sumOneStar / 10000000))}%`
  83.  
  84.     } else if(
  85.         (fiveStarLength || fourStarLength || threeStarLength | twoStarLength || sumTwoStar || oneStarLength) >= 10 ||
  86.         (fiveStarLength || fourStarLength || threeStarLength | twoStarLength || sumTwoStar || oneStarLength) < 11) {
  87.  
  88.         persensFiveStar = `${Math.floor((sumFiveStar / 100000000))}%`
  89.         persenFourStar = `${Math.floor((sumFourStar / 100000000))}%`
  90.         persenThreeStar = `${Math.floor((sumThreeStar / 100000000))}%`
  91.         persenTwoStar = `${Math.floor((sumTwoStar / 100000000))}%`
  92.         persenOneStar = `${Math.floor((sumOneStar / 100000000))}%`
  93.  
  94.     } else if(
  95.         (fiveStarLength || fourStarLength || threeStarLength | twoStarLength || sumTwoStar || oneStarLength) >= 11 ||
  96.         (fiveStarLength || fourStarLength || threeStarLength | twoStarLength || sumTwoStar || oneStarLength) < 12) {
  97.  
  98.         persensFiveStar = `${Math.floor((sumFiveStar / 1000000000))}%`
  99.         persenFourStar = `${Math.floor((sumFourStar / 1000000000))}%`
  100.         persenThreeStar = `${Math.floor((sumThreeStar / 1000000000))}%`
  101.         persenTwoStar = `${Math.floor((sumTwoStar / 1000000000))}%`
  102.         persenOneStar = `${Math.floor((sumOneStar / 1000000000))}%`
  103.    
  104.     } else if(
  105.         (fiveStarLength || fourStarLength || threeStarLength | twoStarLength || sumTwoStar || oneStarLength) >= 12 ||
  106.         (fiveStarLength || fourStarLength || threeStarLength | twoStarLength || sumTwoStar || oneStarLength) < 13) {
  107.  
  108.         persensFiveStar = `${Math.floor((sumFiveStar / 10000000000))}%`
  109.         persenFourStar = `${Math.floor((sumFourStar / 10000000000))}%`
  110.         persenThreeStar = `${Math.floor((sumThreeStar / 10000000000))}%`
  111.         persenTwoStar = `${Math.floor((sumTwoStar / 10000000000))}%`
  112.         persenOneStar = `${Math.floor((sumOneStar / 10000000000))}%`
  113.    
  114.     } else {
  115.         persensFiveStar = '100%'
  116.         persenFourStar = '100%'
  117.         persenThreeStar = '100%'
  118.         persenTwoStar = '100%'
  119.         persenOneStar = '100%'
  120.     }
  121.  
  122.     return { persensFiveStar, persenFourStar, persenThreeStar, persenThreeStar, persenTwoStar, persenOneStar}
  123. }
  124.  
  125. RatingStar.prototype.setRating = function() {
  126.  
  127.    // calculate rating
  128.    const sumFiveStar = this.fiveStar.reduce((a, c) => a + c, 0)
  129.    const sumFourStar = this.fourStar.reduce((a, c) => a + c, 0)
  130.    const sumThreeStar = this.threeStar.reduce((a, c) => a + c, 0)
  131.    const sumTwoStar = this.twoStar.reduce((a, c) => a + c, 0)
  132.    const sumOneStar = this.oneStar.reduce((a, c) => a + c, 0)
  133.  
  134.    // get count people give rating star
  135.    const countFiveStart = this.fiveStar.length + 1
  136.    const countFourStart = this.fourStar.length + 1
  137.    const countThreeStart = this.threeStar.length + 1
  138.    const countTwoStart = this.twoStar.length + 1
  139.    const countOneStart = this.oneStar.length + 1
  140.  
  141.    // mutiply rating with diference rating star
  142.    const multiplyFiveStar = Math.floor(sumFiveStar) * 5
  143.    const multiplyFourStar = Math.floor(sumFourStar) * 4
  144.    const multiplyThreeStar = Math.floor(sumThreeStar) * 3
  145.    const multiplyTwoStar = Math.floor(sumTwoStar) * 2
  146.    const multiplyOneStar = Math.floor(sumOneStar) * 1
  147.  
  148.    // sum rating
  149.    const ratingStarBeforeSum = Math.floor(sumFiveStar + sumFourStar + sumThreeStar + sumTwoStar + sumOneStar)
  150.    const ratingStarAfterSum = (multiplyFiveStar + multiplyFourStar + multiplyThreeStar + multiplyTwoStar + multiplyOneStar)
  151.    const totalPeopleGiveRating = (countFiveStart + countFourStart + countThreeStart + countTwoStart + countOneStart)
  152.  
  153.    // percentage rating by length count
  154.    const persen = this.percentage(sumFiveStar, sumFourStar, sumThreeStar, sumTwoStar, sumOneStar)
  155.  
  156.    const totalRating = +parseFloat((ratingStarAfterSum / ratingStarBeforeSum)).toFixed(1)
  157.    return {
  158.     fiveStar: {
  159.       persen: persen.persensFiveStar,
  160.       peopleGiveRating: countFiveStart
  161.     },
  162.     fourStar: {
  163.       persen: persen.persenFourStar,
  164.       peopleGiveRating: countFourStart
  165.     },
  166.     threeStar : {
  167.       persen: persen.persenThreeStar,
  168.       peopleGiveRating: countThreeStart
  169.     },
  170.     twoStar: {
  171.       persen: persen.persenTwoStar,
  172.       peopleGiveRating: countTwoStart
  173.     },
  174.     oneStar: {
  175.       persen: persen.persenOneStar,
  176.       peopleGiveRating: countOneStart
  177.     },
  178.     totalPeopleGiveRating,
  179.     totalRating
  180.    }
  181. }
  182.  
  183. RatingStar.prototype.getRating = function() {
  184.  
  185.     const data = this.setRating()
  186.  
  187.     if(data.totalRating > 0 && data.totalRating == 1 && data.totalRating < 2) {
  188.         this.rating = 1
  189.  
  190.     } else if(data.totalRating > 1 && data.totalRating == 2 && data.totalRating < 3) {
  191.         this.rating = 2
  192.  
  193.     } else if(data.totalRating > 2 && data.totalRating == 3 && data.totalRating < 4) {
  194.         this.rating = 3
  195.  
  196.     } else if(data.totalRating > 3 && data.totalRating == 4 && data.totalRating < 5) {
  197.         this.rating = 4
  198.  
  199.     } else if(data.totalRating > 4 && data.totalRating == 5 && data.totalRating >= 5) {
  200.         this.rating = 5
  201.  
  202.     } else if(data.totalRating > 0 && data.totalRating < 1) {
  203.         this.rating = 1.5
  204.  
  205.     } else if(data.totalRating > 1 && data.totalRating < 2) {
  206.         this.rating = 2.5
  207.  
  208.     } else if(data.totalRating > 2 && data.totalRating < 3) {
  209.         this.rating = 3.5
  210.  
  211.     } else if(data.totalRating > 3 && data.totalRating < 4) {
  212.         this.rating = 4.5
  213.  
  214.     } else if(data.totalRating > 4 && data.totalRating < 5) {
  215.         this.rating = 5.5
  216.  
  217.     } else {
  218.         rating = 'tidak ada rating'
  219.     }
  220.  
  221.     return Object.assign(data, {ratingStar: this.rating})  
  222. }
  223.  
  224. // example dummy data
  225. const fiveStar = [5, 5, 5, 5, 5, 5 , 5, 5, 5, 5, 5, 5, 5, 5 , 5, 5,
  226. 5, 5, 5, 5, 5, 5 , 5, 5, 5, 5, 5, 5, 5, 5 , 5, 5,
  227. 5, 5, 5, 5, 5, 5 , 5, 5, 5, 5, 5, 5, 5, 5 , 5, 5,
  228. 5, 5, 5, 5, 5, 5 , 5, 5, 5, 5, 5, 5, 5, 5 , 5, 5,
  229. 5, 5, 5, 5, 5, 5 , 5, 5, 5, 5, 5, 5, 5, 5 , 5, 5,
  230. 5, 5, 5, 5, 5, 5 , 5, 5, 5, 5, 5, 5, 5, 5 , 5, 5,
  231. 5, 5, 5, 5, 5, 5 , 5, 5, 5, 5, 5, 5, 5, 5 , 5, 5,
  232. 5, 5, 5, 5, 5, 5 , 5, 5, 5, 5, 5, 5, 5, 5 , 5, 5,
  233. 5, 5, 5, 5, 5, 5 , 5, 5, 5, 5, 5, 5, 5, 5 , 5, 5,
  234. 5, 5, 5, 5, 5, 5 , 5, 5, 5, 5, 5, 5, 5, 5 , 5, 5,
  235. 5, 5, 5, 5, 5, 5 , 5, 5, 5, 5, 5, 5, 5, 5 , 5, 5,
  236. 5, 5, 5, 5, 5, 5 , 5, 5, 5, 5, 5, 5, 5, 5 , 5, 5,
  237. 5, 5, 5, 5, 5, 5 , 5, 5, 5, 5, 5, 5, 5, 5 , 5, 5,
  238. 5, 5, 5, 5, 5, 5 , 5, 5, 5, 5, 5, 5, 5, 5 , 5, 5,
  239. 5, 5, 5, 5, 5, 5 , 5, 5, 5, 5, 5, 5, 5, 5 , 5, 5,
  240. 5, 5, 5,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.5,
  353. 1.5, 1.5 ,1, 1.5, 1.5, 1, 1, 1, 1, 1.5,
  354. 1.5, 1.5 ,1, 1.5, 1.5, 1, 1, 1, 1, 1.5,
  355. 1.5, 1.5 ,1, 1.5, 1.5, 1, 1, 1, 1, 1.5,
  356. 1.5, 1.5 ,1, 1.5, 1.5, 1, 1, 1, 1, 1.5,
  357. 1.5, 1.5 ,1, 1.5, 1.5, 1, 1, 1, 1, 1.5,
  358. 1.5, 1.5 ,1, 1.5, 1.5, 1, 1, 1, 1, 1.5,
  359. 1.5, 1.5 ,1, 1.5, 1.5, 1, 1, 1, 1, 1.5,
  360. 1.5, 1.5 ,1, 1.5, 1.5, 1, 1, 1, 1, 1.5,
  361. 1.5, 1.5 ,1, 1.5, 1.5, 1, 1, 1, 1, 1.5,
  362. 1.5, 1.5 ,1, 1.5, 1.5, 1, 1, 1, 1, 1.5,
  363. 1.5, 1.5 ,1, 1.5, 1.5, 1, 1, 1, 1, 1.5,
  364. 1.5, 1.5 ,1, 1.5, 1.5, 1, 1, 1, 1, 1.5,
  365. 1.5, 1.5 ,1, 1.5, 1.5, 1, 1, 1, 1, 1.5,
  366. 1.5, 1.5 ,1, 1.5, 1.5, 1, 1, 1, 1, 1.5,
  367. 1.5, 1.5 ,1, 1.5, 1.5, 1, 1, 1, 1, 1.5,
  368. 1.5, 1.5 ,1, 1.5, 1.5, 1, 1, 1, 1, 1.5,
  369. 1.5, 1.5 ,1, 1.5, 1.5, 1, 1, 1, 1, 1.5,
  370. 1.5, 1.5 ,1, 1.5, 1.5, 1, 1, 1, 1, 1.5,
  371. 1.5, 1.5 ,1, 1.5, 1.5, 1, 1, 1, 1, 1.5,
  372. 1.5, 1.5 ,1, 1.5, 1.5, 1, 1, 1, 1, 1.5,
  373. 1.5, 1.5 ,1, 1.5, 1.5, 1, 1, 1, 1, 1.5]
  374.  
  375. const data = new RatingStar(fiveStar, fourStar, threeStar, twoStar, oneStar)
  376. console.log(data.getRating())
  377.  
  378. // output
  379. // {
  380. //   fiveStar: { persen: '24%', peopleGiveRating: 481 },
  381. //   fourStar: { persen: '16%', peopleGiveRating: 391 },
  382. //   threeStar: { persen: '7%', peopleGiveRating: 253 },
  383. //   twoStar: { persen: '3%', peopleGiveRating: 172 },
  384. //   oneStar: { persen: '4%', peopleGiveRating: 381 },
  385. //   totalPeopleGiveRating: 1678,
  386. //   totalRating: 3.9,
  387. //   ratingStar: 4.5
  388. // }
Add Comment
Please, Sign In to add comment