简体   繁体   English

作为本练习的解决方案,这两个循环之间的主要区别是什么? (学习javascript)

[英]What is the main difference between these two loops as a solution to this exercise? (learning javascript)

I am learning JS and came across this exercise:我正在学习 JS 并遇到了这个练习:

Write a loop which prompts for a number greater than 100. If the visitor enters another number – ask them to input again.编写一个循环,提示输入一个大于 100 的数字。如果访问者输入了另一个数字——请他们再次输入。

The loop must ask for a number until either the visitor enters a number greater than 100 or cancels the input/enters an empty line.循环必须要求一个数字,直到访问者输入一个大于 100 的数字或取消输入/输入一个空行。

Here we can assume that the visitor only inputs numbers.这里我们可以假设访问者只输入数字。 There's no need to implement a special handling for a non-numeric input in this task.在此任务中无需对非数字输入实施特殊处理。

My solution works and was as follows:我的解决方案有效,如下所示:

let value;
while (true)    {
    value = prompt('Enter a number greater than 100', 0);
    if (value > 100 || value === '');
    console.log(value);
    break;
} 

The MDN solution was this, and though it is shorter and more simple, it seems to accomplish the same task. MDN 解决方案就是这样,虽然它更短更简单,但它似乎完成了相同的任务。

let num;

do {
  num = prompt("Enter a number greater than 100?", 0);
} while (num <= 100 && num);

Is my solution still valid?我的解决方案仍然有效吗? Is the MDN one more proper? MDN 更合适吗?

I just want to make sure I am understanding things correctly as I go.我只是想确保我在做的时候正确理解事情。

If your solution worked before, I think you probably typed in your solution incorrectly here.如果您的解决方案以前有效,我认为您可能在此处错误地输入了您的解决方案。 I am going to assume you meant to write:我假设你打算写:

 if (value > 100 || value === '') {
    console.log(value);
    break;
 }

Since you're just starting out with JS, you will eventually learn that there will be multiple ways you can handle any given coding problem.由于您刚刚开始使用 JS,您最终会了解到有多种方法可以处理任何给定的编码问题。 It will not always be a good answer/wrong answer type of scenario.它并不总是一个好的答案/错误的答案类型的场景。 Sometimes there are multiple ways to accomplish the same thing.有时有多种方法可以完成同一件事。

In this example, the MSN solution is better in terms of readability and possibly safety.在这个例子中,MSN 解决方案在可读性和可能的​​安全性方面更好。

The MSN solution creates a while loop with the exit condition identified in the while statement. MSN 解决方案使用 while 语句中标识的退出条件创建一个 while 循环。 This loop will exit when that condition is met.当满足该条件时,此循环退出。

In your solution, the loop will never exit on it's own, the while() statement will always evaluate to 'true'.在您的解决方案中,循环永远不会自行退出,while() 语句将始终评估为“true”。 This loop needs an explicit exit statement, which you provide with the if() condition.这个循环需要一个显式的退出语句,你可以用 if() 条件提供它。

Your method, although it works, is a little bit less safe in terms of code readability and overall maintenance profile.您的方法虽然有效,但在代码可读性和整体维护配置文件方面不太安全。 For example, a future developer could by mistake change the if() condition and inadvertently create a never ending loop.例如,未来的开发人员可能会错误地更改 if() 条件并无意中创建一个永无止境的循环。

Or, if the loop contained several dozen lines of code, a developer may miss the if condition, and may add some important code after the if condition (such code would not execute when the exit condition is met.)或者,如果循环包含几十行代码,开发人员可能会错过 if 条件,并且可能会在 if 条件之后添加一些重要代码(这些代码在满足退出条件时不会执行。)

Yes, this specific sample exercise is trivial, so the code complexity and readability may not matter.是的,这个特定的示例练习是微不足道的,因此代码复杂性和可读性可能无关紧要。 But in large enterprise applications with hundreds of lines of code, such code choices carry serious risks with costly implications.但是在具有数百行代码的大型企业应用程序中,这样的代码选择会带来严重的风险和代价高昂的影响。

That said, I'll reiterate - as you learn more about JS, you will often find that there are multiple ways of solving any given problem.也就是说,我会重申 - 随着您对 JS 的了解越来越多,您经常会发现有多种方法可以解决任何给定的问题。 Sometimes you do want to create an explicit exit condition through an if() statement, on rare occasions you will want to create a never ending loop.有时您确实希望通过 if() 语句创建一个明确的退出条件,但在极少数情况下您会希望创建一个永无止境的循环。

As you explore more complex problems, you will find needs for such solutiosn.当您探索更复杂的问题时,您会发现对此类解决方案的需求。 So keep learning, keep trying different solutions, and keep asking questions.所以不断学习,不断尝试不同的解决方案,不断提出问题。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM