【问题标题】:how could i reduce if else statements [closed]我如何减少 if else 语句[关闭]
【发布时间】:2014-01-15 09:38:44
【问题描述】:

我写了一个函数来在三个链接之间放置“,”和“and” 我如何减少 if else 语句。 在 javascript 中,如果计数不为零,我会得到计数,这意味着链接必须显示,否则应该隐藏

在以下场景中

function inst_grammer()
{
var otherCount = parseInt($('.global_other_count').html());
var initCount = parseInt($('.global_init_count').html());
var signCount = parseInt($('.global_sign_count').html());

var init_class = $('.inst_init');
var sign_class =  $('.inst_sign');

if (signCount != 0 && initCount != 0 && otherCount == 0)
{
    init_class.html('').fadeOut();
    sign_class.html(' and ').fadeIn();
} else if (signCount == 0 && initCount != 0 && otherCount != 0)
{
    init_class.html(' and ').fadeIn();
    sign_class.html('');
} else if (signCount != 0 && initCount != 0 && otherCount != 0)
{
    init_class.html(' and ').fadeIn();
    sign_class.html(' , ').fadeIn();
}
else if (signCount != 0 && initCount == 0 && otherCount == 0)
{
    init_class.html('').fadeOut();
    sign_class.html('').fadeOut();
}
else if (signCount == 0 && initCount != 0 && otherCount == 0)
{
    init_class.html('').fadeOut();
    sign_class.html('').fadeOut();
}
else if (signCount == 0 && initCount == 0 && otherCount != 0)
{
    init_class.html('').fadeOut();
    sign_class.html('').fadeOut();
}
else if (signCount != 0 && initCount == 0 && otherCount != 0)
{
    init_class.html('').fadeOut();
    sign_class.html(' and ').fadeIn();
}
}

【问题讨论】:

  • 使用 switch 语句。
  • @MahmoodRehman 也许你应该提供一个如何打开 3 个变量组合的示例?
  • 将它们放在一个数组中并使用循环...这只是一个简单的问题,在所选项目之间附加“and”并淡出具有 0 值的元素。
  • 我想减少代码,

标签: javascript php jquery


【解决方案1】:

我认为每个人都以错误的方式看待这个问题。这不是关于简化if,而是关于在语法序列中插入“,”和“和”分隔符的算法。

这个问题的任何解决方案都应该允许任意数量的项目(不仅仅是指定的 3 个)。否则,如果规范发生变化,您可能会获得大量的 if 测试。当然更可重用(即,如果业务需求发生变化)。

我认为,在此示例中,目的是提供一个显示这些选项的显示:

  • “a、b 和 c”
  • “a 和 b”
  • “a 和 c”
  • “b 和 c”
  • “一个”
  • “b”
  • “c”

所以规则是:

  • 如果显示项数为1,则不显示分隔符
  • 如果显示项数为2,则在项之间显示“and”
  • 如果项目数为 3,则使用“,”而不是“和”,最后一项除外。

所以基本上对于 n > 1,最后一个分隔符是“and”,所有其他分隔符都是“,”。这个简单的规则可以应用于任意数量的项目。

你可以通过简单地计算非零项的数量来获得这种效果。 正如我在评论中提到的,将您的数据放在一个数组中,以便您可以简单地对其进行迭代。这意味着您的输出字段也应该在一个数组中,这样您就只按顺序显示您想要的那些。

如果您愿意提供 HTML 示例,很乐意提供代码,但您应该能够从这些简化的规则中自己弄清楚这一点。 :)

【讨论】:

  • +1 用于解决根本问题而不是效果。
【解决方案2】:

您可以使用与 then 代码块对应的函数出错, 然后像这样计算数组的索引:

$index = 4*(signCount%2) + 2*(initCount%1) + (otherCount%2);
$then[$index]();

【讨论】:

  • 你真的计算过这产生的组合数量吗?他想简化他的代码。
  • @TrueBlueAussie:当然。 8种组合。并且有几个有相同的动作
  • 现在,如果您添加此解决方案所需的其余代码,我们可以看看它是否实际上是更少的代码? :)
【解决方案3】:

更新:一个更简单的解决方案是连接您的 3 个变量(1 表示真,0 表示假):

var mycode = "" + (signCount) ? "1":"0" + (initCount)?"1":"0" + (otherCount)?"1":"0"; // Concatenate as string
switch(mycode) {
case "111":
    init_class.html(' and ').fadeIn();
    sign_class.html(' , ').fadeIn();
    break;
case "110":
    init_class.html('').fadeOut();
    sign_class.html(' and ').fadeIn();
    break;
case "101":
    init_class.html('').fadeOut();
    sign_class.html(' and ').fadeIn();
    break;
case "100":
    init_class.html('').fadeOut();
    sign_class.html('').fadeOut();
    break;
case "011":
    init_class.html(' and ').fadeIn();
    sign_class.html('');
    break;
case "010":
    init_class.html('').fadeOut();
    sign_class.html('').fadeOut();
    break;
case "001":
    init_class.html('').fadeOut();
    sign_class.html('').fadeOut();
    break;
}

原始答案:这更容易理解并注意到可能的错误:

if (signCount) {
    if(initCount) {
        if(otherCount) {
            init_class.html(' and ').fadeIn();
            sign_class.html(' , ').fadeIn();
        }
        else {
            init_class.html('').fadeOut();
            sign_class.html(' and ').fadeIn();
        }
    }
    else {
        if(otherCount) {
            init_class.html('').fadeOut();
            sign_class.html(' and ').fadeIn();
        }
        else {
            init_class.html('').fadeOut();
            sign_class.html('').fadeOut();
        }
    }
}
else {
    if (initCount) {
        if(otherCount) {
            init_class.html(' and ').fadeIn();
            sign_class.html('');
        }
        else {
            init_class.html('').fadeOut();
            sign_class.html('').fadeOut();
        }
    }
    else {
        if(otherCount) {
            init_class.html('').fadeOut();
            sign_class.html('').fadeOut();
        }
    }
}

除此之外,恐怕没有简单的方法可以简化这个结。

【讨论】:

  • 您的开关假定它们只有 0 或 1 的计数(我认为它们的值是 0 或更大)。
  • 只有当值与 0 不同时,OP 才感兴趣,所以这似乎是有效的。在答案中添加了将值转换为布尔值。谢谢。
【解决方案4】:

这个选项怎么样?

$arr = array(
    array( 1, 1, 1 ), array( 1, 1, 0 ), array( 1, 0, 0 ), array( 0, 0, 0 ),
    array( 0, 0, 1 ), array( 0, 1, 1 ), array( 1, 0, 1 ), array( 0, 1, 0 )
);
$option = array_search( array($signCount?1:0, $initCount?1:0, $otherCount?1:0 );

switch( $option, $arr ) ) {
    case 0:
        init_class.html(' and ').fadeIn();
        sign_class.html(' , ').fadeIn();
        break;
    case 1:
    case 6:
        init_class.html('').fadeOut();
        sign_class.html(' and ').fadeIn();
        break;
    case 2:
    case 4:
    case 7:
        init_class.html('').fadeOut();
        sign_class.html('').fadeOut();
        break;
    case 3: // none
    break;
    case 5:
        init_class.html(' and ').fadeIn();
        sign_class.html('');
        break;
}

【讨论】:

  • 很适合组合相同的结果。 (y)
【解决方案5】:

试试这个。我刚刚使用 ||对于某些条件,因为它们无论如何都在做同样的工作。

特别是这份工作

init_class.html('').fadeOut();
sign_class.html(' and ').fadeIn();

还有这份工作

init_class.html('').fadeOut();
sign_class.html('').fadeOut();

在您的代码中被多次调用。所以我只是使用||对于那些条件。

if ((signCount != 0 && initCount != 0 && otherCount == 0) || (signCount != 0 && initCount == 0 && otherCount != 0))
{
    init_class.html('').fadeOut();
    sign_class.html(' and ').fadeIn();
}
else if (signCount == 0 && initCount != 0 && otherCount != 0)
{
    init_class.html(' and ').fadeIn();
    sign_class.html('');
}
else if (signCount != 0 && initCount != 0 && otherCount != 0)
{
    init_class.html(' and ').fadeIn();
    sign_class.html(' , ').fadeIn();
}
else if ((signCount != 0 && initCount == 0 && otherCount == 0) || (signCount == 0 && initCount != 0 && otherCount == 0) || (signCount == 0 && initCount == 0 && otherCount != 0))
{
    init_class.html('').fadeOut();
    sign_class.html('').fadeOut();
}

【讨论】:

  • 这似乎几乎无法调试。
  • 是的,相同的结果 scnerios 应该在 else 语句中。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2012-05-11
  • 2017-09-05
  • 2019-08-20
  • 2010-12-20
  • 2016-10-15
  • 2016-02-28
  • 2018-03-29
相关资源
最近更新 更多