【问题标题】:Having trouble conceptualizing how i would change this if else statement into a switch statement在概念化如何将 if else 语句更改为 switch 语句时遇到问题
【发布时间】:2019-10-15 19:10:57
【问题描述】:

在将很长的 if/else if/else 语句更改为 switch case 语句时,我需要帮助。从概念上讲,我不确定应该在 switch 表达式中添加什么

if (!$jq('*').hasClass("answerPick")) {
    // if nothing is selected 
     finalURL= "productResults.aspx?N=1320325";
}
else if (q1 === undefined && q2 === undefined && q3Multi === undefined) {
    // if q1 a2 and q3 arent selected 
    finalURL= "productResults.aspx?N=1320325+" + q4Multi.join('+');
}
else if (q1 != undefined && q2 === undefined && q3Multi === undefined) {
    // if q1 a2 and q3 arent selected 
    finalURL= "productResults.aspx?N=1320325+" q15 + q4Multi.join('+');
}

else if (q1 === undefined && q2 === undefined) {
    // if q1 and q2 arent selected 
     finalURL= "productResults.aspx?N=1320325+" + q3Multi + q4Multi.join('+');
}
else if (q2 === undefined && q1 != undefined) {
    // if only q2 is left unselected 
      finalURL = "productResults.aspx?N=" + q15 + q3Multi + q4Multi.join('+');
}
else if (q2 === undefined && q3Multi === undefined) {
    // if only q1 is selected 
     finalURL = "productResults.aspx?N=" + q15;
}
else{
    // if everything is selected
      finalURL = "productResults.aspx?N=" + q2 + q3Multi + q4Multi.join('+');
}

switch(expression) {
  case n:
    code block
    break;
  case n:
    code block
    break;
  default:
    default code block
} 

请让我知道我应该包括的其他任何内容。我认为我错过了一些非常明显的东西,但从概念上讲它并没有点击。

【问题讨论】:

  • switch 用于针对多个值测试单个表达式。这不合适。
  • 如果要简化,可以使用嵌套测试。
  • 您可以使用switch (true),然后使用case q1 === undefined && q2 === undefined && q3Multi === undefined:,但这并不是对任何事情的简化。
  • 非常感谢 Barmar。

标签: javascript if-statement switch-statement


【解决方案1】:

您可以将变量设置为指示定义了哪些变量的组合,然后打开它。

if (!$jq('*').hasClass("answerPick")) {
    // if nothing is selected 
    finalURL= "productResults.aspx?N=1320325";
} else {
    var selected = (q1 === undefined ? "q1undef" : "q1def") + "+" + (q2 === undefined ? "q2undef" : "q2def") + "+" + (q3Multi === undefined ? "q3undef" : "q3def");
    switch(selected) {
    case "q1undef+q2undef+q3undef":
        // if q1 a2 and q3 arent selected 
        finalURL= "productResults.aspx?N=1320325+" + q4Multi.join('+');
        break;
    case "q1def+q2undef+q3undef":
        finalURL= "productResults.aspx?N=1320325+" q15 + q4Multi.join('+');
        break;
    case "q1undef+q2undef+q3def":
    case "q1undef+q2undef+q3undef":
        // if q1 and q2 arent selected 
        finalURL= "productResults.aspx?N=1320325+" + q3Multi + q4Multi.join('+');
        break;
    case "q1def+q2undef+q3def":
    case "q1def+q2undef+q3undef":
        // if only q2 is left unselected 
        finalURL = "productResults.aspx?N=" + q15 + q3Multi + q4Multi.join('+');
        break;
    case "q1def+q2undef+q3undef":
    case "q1undef+q2undef+q3undef":
        // if only q1 is selected 
        finalURL = "productResults.aspx?N=" + q15;
        break;
    default:
        // if everything is selected
        finalURL = "productResults.aspx?N=" + q2 + q3Multi + q4Multi.join('+');
        break;
    }
}

【讨论】:

  • 谢谢巴尔玛。一个问题,我对条件三元运算符不是很熟悉,但我看到你有 (q1 === undefined ? "q1def" : "q1undef") MDN 将三元运算符的语法显示为:条件? exprIfTrue : exprIfFalse 考虑到这一点,它应该是这样的 - (q1 === undefined ? "q1undef" : "q1def") 吗?我离开了我的工作站,目前无法自己尝试。
  • 是的,我把它们弄反了。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2019-07-03
  • 1970-01-01
  • 2019-07-04
  • 2022-08-23
  • 1970-01-01
相关资源
最近更新 更多