【问题标题】:How to manage multiple if and else in javascript如何在javascript中管理多个if和else
【发布时间】:2019-07-30 16:01:27
【问题描述】:

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

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

【问题讨论】:

  • 请详细说明您面临的问题是什么?
  • 你似乎没有真正问过任何问题?
  • 他的问题在标题中,我猜他的代码有效,但意识到有更好的方法。我猜他正在寻找开关盒的解释。
  • 您错过了实际问题。 stackoverflow.com/help/how-to-ask
  • 你的第一个 if(time === "no") allways if time is no 会显示警报.....

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


【解决方案1】:

我的第一个建议是使用 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') ) {

可以重写为just

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

【讨论】:

    【解决方案2】:

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

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

    【讨论】:

      猜你喜欢
      • 2011-03-15
      • 1970-01-01
      • 1970-01-01
      • 2016-08-05
      • 1970-01-01
      • 1970-01-01
      • 2018-04-24
      • 2012-10-23
      相关资源
      最近更新 更多