javascript - 如何在javascript中管理多个if和else

标签 javascript html if-statement conditional-statements

这个程序打印出“Nobody is busy its just a matter of PRIORITIES”。如果用户对时间问题回答“否”,我希望它说如果您对时间问题回答“否”并且对至少一个其他问题回答"is"。如果您对所有问题都回答“否”,则应改为“您不是来 self 们”。我不确定如何更改此代码以获得此结果。

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

最佳答案

我的第一个建议是使用 true/false bool 值而不是检查字符串 "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');
  }
}

关于javascript - 如何在javascript中管理多个if和else,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55066290/

相关文章:

二叉搜索树上的 JavaScript 大小

jquery滚动问题

vba - VBA 中如何计算 Or 语句?

javascript - 结帐时出现 WooCommerce JS 问题

javascript - WebSocket 客户端无法通过企业代理连接到云服务器,除了网络浏览器! (超时)

javascript - IE 中的 jQuery 跨域发布与数据

html - Flexbox 宽度随内容变化,应固定宽度

javascript - 剑道网格 : How to disable "filterable" on data-column definition?

matlab - MATLAB 中的条件 "Or"语句

django - 如何在 Django DetailView 中使用来自 URL 和请求的值?