简体   繁体   English

我想使用 HTML 和 JavaScript 执行 While 循环,但是使用我当前的代码循环并没有结束

[英]I would like to do a While loop using HTML and JavaScript however with my current code the loop is not ending

Currently the loop is running constantly.目前,循环一直在运行。 It seems my else function is not being read properly.似乎我的 else 函数没有被正确读取。 Could someone please send some assistance.有人可以请提供一些帮助。

Also when I run it on Google Chrome, the first thing to pop up is a "Hello Null" box meaning that the window.alert function is running automatically.此外,当我在 Google Chrome 上运行它时,首先弹出的是一个“Hello Null”框,这意味着 window.alert 功能正在自动运行。

<html>
  <head>
    <title>JavaScript Example</title>
    <script>
      "use strict";
        function start() {
          let ino = window.prompt("What is your name?");
          window.alert("Hello " + ino);
          while (ino != "end") {
            let ino = window.prompt("What is your name?");
            window.alert("Hello " + ino);}
        else {window.alert("go away now mate"); }
            }

    </script>
  </head>
  <body onLoad="start()"> 
    <p>Javascript Test Page</p>
  </body>
</html>

It looks to me like you are trying to prompt until the user identifies themselves as "end".在我看来,您正在尝试提示,直到用户将自己标识为“结束”。 If that's the case, you probably want a do...while around the prompt.如果是这种情况,您可能想要在提示周围做...while。

let name;
do {
    name = window.prompt....;
} while (name !== 'end');

Note that you are mixing 2 goals without a well defined usecase.请注意,您在没有明确定义的用例的情况下混合了 2 个目标。 If you want to tell them to go away mate should you be using a while loop?如果你想告诉他们go away mate你应该使用while循环吗? A while loop sounds like you want to keep asking the question. while 循环听起来像是您想继续问这个问题。 The if...else makes it seem like you want to validate their entry and reject if it's not "end". if...else 看起来像是您想要验证他们的输入并reject如果它不是“结束”。 If your actual goal is to ask the user twice then keep a count and use the count as a condition in your while loop.如果您的实际目标是询问用户两次,则保留一个计数并将该计数用作 while 循环中的条件。 while (name !== 'end' && count < 3) . while (name !== 'end' && count < 3) You can can tell the user to "buzz off" after your while loop is complete AND there's name is still not end您可以告诉用户在 while 循环完成后“嗡嗡作响”并且名称仍未end

Your other question, it makes sense that it shows your prompt as soon as the page loads because you are binding it to onLoad event.您的另一个问题,它在页面加载后立即显示您的提示是有道理的,因为您将其绑定到 onLoad 事件。 Shouldn't it?不应该吗?

Watching at louis answer;看着路易斯回答; I have similar questions and added the code below as an example implementation.我有类似的问题,并添加了下面的代码作为示例实现。 please try it and use the console browser to check if there aren't any errors next time.请尝试并在下次使用控制台浏览器检查是否有任何错误。 Before adding the code online.在线添加代码之前。

  • also pay attention to indents, and be consistent with them below is an example.还要注意缩进,并与它们保持一致下面是一个例子。
  • use variables which are readable, another developer might not know what ino means.使用可读的变量,其他开发人员可能不知道 ino 是什么意思。
<html>
  <head>
    <title>JavaScript Example</title>
    <script>
      "use strict";
        function start() {
            let name = window.prompt("What is your name?");
            let count = 1;

            while (name !== "end" && count <= 3) {
                window.alert("Hello " + name);
                name = window.prompt("What is your name?");
                count++;
            }
            window.alert("go away now mate"); 
        }

    </script>
  </head>
  <body> 
  <button onclick="start()">start</button>
    <p>Javascript Test Page</p>
  </body>
</html>

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

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