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

问题描述 投票:2回答:2

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

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');
}
javascript html if-statement conditional condition
2个回答
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');
  }
}

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');
  }
}
© www.soinside.com 2019 - 2024. All rights reserved.