如何让javascript代码更清晰?

时间:2013-01-30 07:19:18

标签: javascript

我有五星评级和平均分。

  
      
  1. 平均得分为浮点数
  2.   
  3. 五星评级仅10个部分,一颗星有两个半星(所以10个部分是五星级)
  4.   

前:

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]

如何根据平均分数检查并将平均分数设为10个部分?

这是我的代码,很长。

            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; }
            }

非常感谢@ th3falc0n使用小代码,我编写了超过50行代码,但是您可以使用一行代码来解决我的长代码。

感谢大家帮助我,对不清楚的问题和糟糕的英语感到抱歉

3 个答案:

答案 0 :(得分:3)

如果我正确理解了您的问题,您希望获得平均分并将其转换为星级评分,其中每颗星的价值为2分,并且您从average开始,这似乎是一个小数在05之间。

如果是这样的话:

// 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
];

那些是旗帜。使用stars[0]决定是否显示第一颗星,stars[1]以决定是否显示第二颗星等。

如果您的average实际上在010之间,那么只需相应调整数字:

// 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
];

答案 1 :(得分:1)

我认为你只需要

Math.floor(average * 2.0) / 2.0;

此代码仅从任何浮点数中留下x.y.

编辑:我犯了一个小错误。我将其设置为0.1步,因此它将为0.85到0.8,现在已经固定(1 / 2.0)= 0.5步。你可以使用4.0代表0.25步骤10.0代表0.1等等。

答案 2 :(得分: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;
        }