1

I'm creating a rating system, which rounds values to the nearest 0.5 and contains values between 0 and 5.

How can I iterate over an array, splitting a value (rounded to nearest 0.5), into units.

let rating = 3.7

let adjustedRating = (Math.round(rating * 2) / 2);

e.g. 3.7: ['1','1','1','1',0]

1.4 would equal ['1', '0.5', '0', '0', '0']

let starsToShow = new Array().fill('0'); starsToShow.forEach((v, i) => { ... });

5
  • why 1.4 would equal ['1', '0.5', '0', '0', '0'] 1.4 should equal to that ? Commented Jul 26, 2016 at 5:09
  • I'm rounding the rating up to the nearest half value. Commented Jul 26, 2016 at 5:10
  • @PranavCBalan it would return as 2, ['1', '1', '0', '0', '0'] Commented Jul 26, 2016 at 5:15
  • @rickysullivan then 3.7 should be ['1','1','1','1',0] ? Commented Jul 26, 2016 at 5:19
  • @PranavCBalan, whoops. Commented Jul 26, 2016 at 5:38

4 Answers 4

2

Do it using Array.from

let rating = 3.7
let adjustedRating = (Math.round(rating * 2) / 2);

console.log(
  Array.from({
    // set array length here
    length: 5
  }, function(_, i) { // iterate over element to update
    // get the difference with index
    var dif = adjustedRating - i;
    // based on the difference assign the array value
    return dif >= 1 ? '1' : dif > 0 ? '0.5' : '0';
  })
);


With simple for loop

var rating = 3.7,
  res = [],
  adjustedRating = (Math.round(rating * 2) / 2);

for (var i = 0; i < 5; i++) {
  var dif = adjustedRating - i;
  res.push(dif >= 1 ? '1' : dif > 0 ? '0.5' : '0');
}

console.log(res);

Sign up to request clarification or add additional context in comments.

3 Comments

Cool solution. I didn't know about Array.from. Much less code than the one I suggested.
Nice, and using ES 2015 :)
var dif = (Math.round(rating * 2) / 2) - i;
1

Just another entry, using reduce and forEach. from is probably better if less code matters (1 line?), but compatibility may be an issue.

// Using forEach
function getRating0(rating) {
  var stars = [0,0,0,0,0];
  rating = Math.round(rating * 2) / 2;
  stars.forEach(function(v, i) {
    stars[i] = rating > 1? 1 : rating > 0? rating : 0;
    --rating;
  });
  return stars;
}  

// Using reduce
function getRating(rating) {
  var stars = [0,0,0,0,0];
  rating = Math.round(rating * 2) / 2;
  return stars.reduce(function(acc, v, i) {
    stars[i] = rating > 1? 1 : rating > 0? rating : 0;
    --rating;
    return stars;
  }, stars);
}  

console.log(getRating0(1.2));
console.log(getRating0(3.7));
console.log(getRating(1.2));
console.log(getRating(3.9));

Comments

0

$('.calculate-button').click(function(){
  var rating = $('.rating').val();
  var array = new Array();
  while (rating > 0){
    var currentRating;
    if(rating > 1)
    {
      var currentRating = 1;
      rating -= currentRating;
      array.push(currentRating);
    }
    else{
      var currentRating = (Math.round(rating * 2) / 2).toFixed(1);
      rating = 0;
      array.push(parseFloat(currentRating));
  }
}

   while (array.length < 5)
   {
     array.push(0);
   }

  console.log(array);
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<input type="text" class="rating"/>
<button class="calculate-button">Calculate</button>

Comments

0

This could work as you wanted to, but I would prefer if you would take the suggestion of AlienHoboken. It's much cleaner.

let rating = 3.7;
let adjustedRating = (Math.round(rating * 2) / 2);

let tempRating = adjustedRating;
let starsToShow = new Array(5).fill(0);

for(let i=0; i<5; i++) {
  if (tempRating > 0.5) {
    tempRating -= 1.0;
    starsToShow[i] += 1.0;
  } else {
    starsToShow[i]+= 0.5;
    break;
  }
}

console.log(starsToShow);

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.