[英]Having trouble conceptualizing how i would change this if else statement into a switch statement
I need assistance with changing a very long if/else if/else statement into a switch case statement.我需要帮助将很长的 if/else if/else 语句更改为 switch case 语句。 Conceptually im not certain of what should be put in the switch expression
从概念上讲,我不确定应该在 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
}
Please let me know of anything else i should include.请让我知道我应该包括的其他任何内容。 I think im missing something super obvious but conceptually it isnt clicking.
我认为我错过了一些非常明显的东西,但从概念上讲它不是点击。
You can set a variable to a combination that indicates which variables are defined, and then switch on that.您可以将变量设置为指示定义了哪些变量的组合,然后将其打开。
if (!$jq('*').hasClass("answerPick")) {
// if nothing is selected
finalURL= "productResults.aspx?N=1320325";
} else {
var selected = (q1 === undefined ? "q1def" : "q1undef") + "+" + (q2 === undefined ? "q2def" : "q2undef") + "+" + (q3Multi === undefined ? "q3def" : "q3undef");
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;
}
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.