如何在JavaScript中管理多个if and else

时间:2019-03-08 15:27:06

标签: javascript html if-statement conditional conditional-statements

该程序打印出“没有人在忙,这只是优先事项”。如果用户对时间问题的回答为“否”,那么我想说的是,如果您对时间问题的回答为“否”,而对其他至少一个问题的回答为“是”。如果您对所有问题回答“否”,则应改为“您不是我们的”。我不确定如何更改此代码以获得此结果。

var javascript = prompt("Want to learn javascript? (Type yes or no)");
var docker = prompt("Want to learn docker? (Type yes or no)");
var time = prompt ("do you have time ? (type yes or no)");

if(time === "no") {
   alert("Nobody is busy its just a matter of PRIORITIES");
}
if ((javascript ==="yes" && time === "yes") && (docker === 'yes' && time ==='yes') ) {
   alert("keep patience first learn docker and then learn javascript");
}    
else if (javascript === "yes" && docker === "yes") {
   if (time === "no") {
      alert("so what should I do  if u don't have time ?");
   }
}	
else if (javascript ==="yes" && time === "yes") {
   alert("go and learn javascript");
}
else if (time ==='no' && javascript === "yes") {
   alert("\"A smarter way to learn javascript\" will solve your problem in less time ");
}
else if (docker === 'yes' && time ==='yes') {
   alert(' go n learn docker');
}
else if (time ==='no' && docker === "yes") {
   alert("\"Docker Deep Dive\" will solve your problem in less time ");
} 
else {
   alert('You are not from us');
}

2 个答案:

答案 0 :(得分:4)

我的第一个建议是使用true / false布尔值而不是检查字符串"yes""no"来简化代码。我做了一个功能,可以帮助您进行转换。它还可以处理例如有人输入"NO""yEs"时发生的情况。

此外,仅使用一致的格式有助于使代码更易于阅读。

function promptToBoolean(txt) {
  return /yes/i.test(prompt(txt));
}

var javascript = promptToBoolean("Want to learn javascript? (Type yes or no)");
var docker = promptToBoolean("Want to learn docker? (Type yes or no)");
var time = promptToBoolean("do you have time ? (type yes or no)");

if (!time) {
  alert("Nobody is busy its just a matter of PRIORITIES");
}

if (javascript && time && docker) {
  alert("keep patience first learn docker and then learn javascript");
} else if (javascript && docker && !time) {
  alert("so what should I do  if u don't have time ?");
} else if (javascript && time) {
  alert("go and learn javascript");
} else if (!time && javascript) {
  alert("\"A smarter way to learn javascript\" will solve your problem in less time ");
} else if (docker && time) {
  alert('go n learn docker');
} else if (!time && docker) {
  alert("\"Docker Deep Dive\" will solve your problem in less time ");
} else {
  alert('You are not from us');
}


帮助管理它们的另一种方法是更好地遵循逻辑,不要重复自己,也不要使用不必要的括号或嵌套语句。

例如,这个

if ((javascript ==="yes" && time === "yes") && (docker === 'yes' && time ==='yes') ) {

可以重写为

if (javascript && time && docker) {

然后这个:

} else if (javascript === "yes" && docker === "yes") {
    if (time === "no") {
        alert("so what should I do  if u don't have time ?");
    }
}

可以改写为:

} else if (javascript && docker && !time) {
    alert("so what should I do  if u don't have time ?");
}

我还建议将内容分成大块进行管理,例如使用time进行管理,这似乎经常检查,因此您可以只执行一次检查,然后在其中管理其他逻辑代码块

if (time) {
  //Put everything in here where time is true
} else {
  //Put everything in here where time is false
}

赞:

function promptToBoolean(txt) {
  return /yes/i.test(prompt(txt));
}

var javascript = promptToBoolean("Want to learn javascript? (Type yes or no)");
var docker = promptToBoolean("Want to learn docker? (Type yes or no)");
var time = promptToBoolean("do you have time ? (type yes or no)");

if (time) {
  if (javascript && docker) {
    alert("keep patience first learn docker and then learn javascript");
  } else if (javascript) {
    alert("go and learn javascript");
  } else if (docker) {
    alert('go n learn docker');
  }
} else {
  alert("Nobody is busy its just a matter of PRIORITIES");

  if (javascript && docker) {
    alert("so what should I do  if u don't have time ?");
  } else if (javascript) {
    alert("\"A smarter way to learn javascript\" will solve your problem in less time ");
  } else if (docker) {
    alert("\"Docker Deep Dive\" will solve your problem in less time ");
  } else {
    alert('You are not from us');
  }
}

答案 1 :(得分:4)

所有建议和建议都非常有用。我已经更改了问题。如果您对所有问题回答“否”,则应改为“您不是我们的”,并且不应打印出“没人在忙,这只是优先事项”,在这种情况下,只有在时间为“否”的情况下,应该打印“没人忙,这只是优先事项”

function promptToBoolean(txt) {
  return /yes/i.test(prompt(txt));
}

var javascript = promptToBoolean("Want to learn javascript? (Type yes or no)");
var docker = promptToBoolean("Want to learn docker? (Type yes or no)");
var time = promptToBoolean("do you have time ? (type yes or no)");

if (!time) {
  alert("Nobody is busy its just a matter of PRIORITIES");
}

if (javascript && time && docker) {
  alert("keep patience first learn docker and then learn javascript");
} else if (javascript && docker && !time) {
  alert("so what should I do  if u don't have time ?");
} else if (javascript && time) {
  alert("go and learn javascript");
} else if (!time && javascript) {
  alert("\"A smarter way to learn javascript\" will solve your problem in less time ");
} else if (docker && time) {
  alert('go n learn docker');
} else if (!time && docker) {
  alert("\"Docker Deep Dive\" will solve your problem in less time ");
} else {
  alert('You are not from us');
}

function promptToBoolean(txt) {
  return /yes/i.test(prompt(txt));
}

var javascript = promptToBoolean("Want to learn javascript? (Type yes or no)");
var docker = promptToBoolean("Want to learn docker? (Type yes or no)");
var time = promptToBoolean("do you have time ? (type yes or no)");

if (time) {
  if (javascript && docker) {
    alert("keep patience first learn docker and then learn javascript");
  } else if (javascript) {
    alert("go and learn javascript");
  } else if (docker) {
    alert('go n learn docker');
  }
} else {
  alert("Nobody is busy its just a matter of PRIORITIES");

  if (javascript && docker) {
    alert("so what should I do  if u don't have time ?");
  } else if (javascript) {
    alert("\"A smarter way to learn javascript\" will solve your problem in less time ");
  } else if (docker) {
    alert("\"Docker Deep Dive\" will solve your problem in less time ");
  } else {
    alert('You are not from us');
  }
}

function promptToBoolean(txt) {
  return /yes/i.test(prompt(txt));
}

var javascript = promptToBoolean("Want to learn javascript? (Type yes or no)");
var docker = promptToBoolean("Want to learn docker? (Type yes or no)");
var time = promptToBoolean("do you have time ? (type yes or no)");

if (time) {
  if (javascript && docker) {
    alert("keep patience first learn docker and then learn javascript");
  } else if (javascript) {
    alert("go and learn javascript");
  } else if (docker) {
    alert('go n learn docker');
  }
} else {
  alert("Nobody is busy its just a matter of PRIORITIES");

  if (javascript && docker) {
    alert("so what should I do  if u don't have time ?");
  } else if (javascript) {
    alert("\"A smarter way to learn javascript\" will solve your problem in less time ");
  } else if (docker) {
    alert("\"Docker Deep Dive\" will solve your problem in less time ");
  } else {
    alert('You are not from us');
  }
}