0

I have five star rating and average score.

  1. Average score is float number
  2. Five star rating just 10 section, one star have two half star (so 10 section is five star)

Ex:

Average score is: from 0 - 5 (average = 4.658, or average = 0.256, 0 <= average <= 5)

Five star rating is 10 section: [0, 0.5, 1, 1.5, 2, 2.5, 3, 3.5, 4, 4.5, 5]

How to check and set average score to 10 section base on average score?

This is my code, it very long.

            var twofixed = Math.round(average * 100) / 100;           
            twofixed = Math.abs(twofixed);
            var decimal = twofixed - Math.floor(twofixed)

            twofixed = (twofixed + "").split(".");

            if(twofixed[0] == 0)
            {
                if(twofixed[1] == undefined) { average = 0; }
                else
                {
                    if(twofixed[1] <= 99)
                    {
                        average = 0.50;
                    }
                    else
                    {
                        average = 1;
                    }
                }
            }
            else if(twofixed[0] == 1)
            {
                if(twofixed[1] == undefined) { average = 1; }

                else
                {
                    if(twofixed[1] <= 99)
                    {
                        average = 1.50;
                    }
                    else
                    {
                        average = 2;
                    }
                }
            }
            else if(twofixed[0] == 2)
            {
                if(twofixed[1] == undefined) { average = 2; }
                else
                {
                    if(twofixed[1] <= 99)
                    {
                        average = 2.50;
                    }
                    else
                    {
                        average = 3;
                    }
                }
            }
            else if(twofixed[0] == 3)
            {
                if(twofixed[1] == undefined) { average = 3; }
                else
                {
                    if(twofixed[1] <= 99)
                    {
                        average = 3.50;
                    }
                    else
                    {
                        average = 4;
                    }
                }
            }
            else if(twofixed[0] == 4)
            {
                if(twofixed[1] == undefined) { status = 'Great'; average = 4; }
                else
                {
                    if(twofixed[1] <= 99)
                    {
                        average = 4.50;
                    }
                    else
                    {
                        average = 5;
                    }
                }                    
            }
            else if(twofixed[0] == 5)
            {
                if(twofixed[1] == undefined) { average = 5; }
            }

Big thanks to @th3falc0n with small code, I write more than 50 line of code, but you can do with one line of code to solve of my long code.

Thanks everyone to help me, and sorry for unclearly question and bad english

2
  • What is average as of the starting point in your code? Commented Jan 30, 2013 at 7:29
  • Average is the average of rating. Ex: I have 5 user, every user have one rating, then average = 5/5 = 1 Commented Jan 30, 2013 at 8:28

3 Answers 3

3

If I understand your question correctly, you want to take an average score and convert that into a star rating where each star is worth two points, and you're starting with average which appears to be a fractional number between 0 and 5.

If so:

// Flags for whether to show stars, based on `average` being
// a fractional number `0 <= average < 5`
var stars = [
    average > 0,
    average > 1,
    average > 2,
    average > 3,
    average > 4
];

Those are flags. Use stars[0] to decide whether to show the first star, stars[1] to decide whether to show the second, etc.

If your average is actually between 0 and 10, then just adjust the numbers accordingly:

// Flags for whether to show stars, based on `average` being
// a fractional number `0 <= average < 10`
var stars = [
    average > 0,
    average > 2,
    average > 4,
    average > 6,
    average > 8
];
Sign up to request clarification or add additional context in comments.

4 Comments

His code uses 10 steps but you are only providing 5 flags. Please change that.
@th3falc0n: He talks about five stars, so that's what I did. Until/unless the question is clarified... :-)
thanks for you instruction, but it not just integer number. I have Five Star, every star have two half star, but average number is float, so i want to make average fix to 10 section in five star.
Thanks, you anwser another question of me. Sorry for my bad question and bad english
1

I think you just need

Math.floor(average * 2.0) / 2.0;

This code leaves only x.y from any float number.

Edit: I made a little mistake. I set it to 0.1 steps so it would make 0.85 to 0.8, that is fixed now (1/2.0) = 0.5 steps. You can use 4.0 for 0.25 steps 10.0 for 0.1 and so on.

3 Comments

The 'average' just from 0 - 5, but it a decimal number, i want fix it to 10 section, [0, 0.5, 1, 1.5, 2, 2.5, 3, 3.5, 4, 4.5, 5]. Ex: If average = 4.69 then it will 4.5 in 10 section
That is exactly what my code does. It makes 0.12 to 0, 0.54 to 0.5, 1.499 to 1 an so on.
Oh, very simple. Sorry I just again and it work perfect, very small but very effective, thank you so much
0
function getAverage(twofixed)
        {
            var average;
            if(twofixed[1] == undefined)
            {
                average = twofixed[0];
            }
            else
            {
                average = (twofixed[1] <= 99) ? twofixed[0]+0.50 : twofixed[0]+1;
            }
            return average;
        }

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.