使用包含for循环和条件语句的函数在HTML中打印所有数字。

huangapple go评论68阅读模式
英文:

Printing all the numbers in html using a function that contains for loop and conditions statements

问题

我尝试使用一个包含for循环和条件语句的函数来解决FizzBuzz问题,然后将其引用到我的HTML文档中。该函数可以正确计算输入的值,例如,如果输入为1,则显示1,如果输入为3,则显示Fizz等等。问题在于它只打印出了该数字的条件答案,而不是直到输入的那个数字为止的所有数字。如果我输入18,它只显示FizzBuzz,而不显示直到18的所有数字。我不明白我错过了什么或者哪里出了问题,我仍然在努力使用调试器找出原因。

<label for="num">输入一个数字:</label>
<input type="number" id="num" name="num">
<button id="btn">开始</button>
<p id="result"></p>

<script>
  let btn = document.getElementById("btn");
  btn.addEventListener("click", fizzBuzz);

  function fizzBuzz() {
    let number = document.getElementById("num").value;
    for (let i = 1; i <= number; i++) {
      let appendString = "";
      if (i % 3 === 0) {
        appendString += "Fizz";
      }
      if (i % 5 === 0) {
        appendString += "Buzz";
      }
      if (appendString === "") {
        document.getElementById("result").innerHTML = i;
      } else {
        document.getElementById("result").innerHTML = appendString;
      }
    }
  }
</script>
英文:

I've tried to solve the FizzBuzz problem using a function that contains a for loop and conditions statement and then to do references to my html document. The function calculates correct the value from the input, for example it shows 1 if it is 1, fizz if the number is 3 and so on. The issue is that it prints only the answer of the condition for that number and not all the numbers until the one that is introduced. If I type 18, it display only FizzBuzz and not all the numbers until it reaches 18 for example. I don't understand what I miss or what is wrong and I still struggle with using the debugger to find out.

 &lt;label for = &quot;num&quot;&gt;Enter a number: &lt;/label&gt;
 &lt;input type =&quot;number&quot; id =&quot;num&quot; name =&quot;num&quot;&gt;
 &lt;button id = &quot;btn&quot;&gt;Play&lt;/button&gt;
 &lt;p id = &quot;result&quot;&gt;&lt;/p&gt;

 &lt;script&gt;
  let btn = document.getElementById(&quot;btn&quot;);
  btn.addEventListener(&quot;click&quot;, fizzBuzz);

  function fizzBuzz() {
    let number = document.getElementById(&quot;num&quot;).value;
    for (let i=1; i&lt;=number; i++) {
       let appendString = &quot;&quot;;
       if (i%3 === 0) {
        appendString += &quot;Fizz&quot;;
       }
       if (i%5 === 0) {
        appendString += &quot;Buzz&quot;;
       }
       if (appendString === &quot;&quot;) {
        document.getElementById(&quot;result&quot;).innerHTML = i;  
       } else {
        document.getElementById(&quot;result&quot;).innerHTML = appendString;
       }
     }
  }
    &lt;/script&gt;

答案1

得分: 2

欢迎来到StackExchange!

查看您的代码,看起来您正在使用=来设置innerHtml,这将用新值覆盖内容。您需要将其替换为+=(追加,因此x += 1等同于x = x + 1

<label for="num">输入一个数字:</label>
<input type="number" id="num" name="num">
<button id="btn">播放</button>
<p id="result"></p>

<script>
  let btn = document.getElementById("btn");
  btn.addEventListener("click", fizzBuzz);

  function fizzBuzz() {
    let number = document.getElementById("num").value;
    for (let i=1; i<=number; i++) {
      let appendString = "";
      if (i%3 === 0) {
        appendString += "Fizz";
      }
      if (i%5 === 0) {
        appendString += "Buzz";
      }
      // !! 我们将这两个从 = 改为了 +=
      if (appendString === "") {
        document.getElementById("result").innerHTML += i;
      } else {
        document.getElementById("result").innerHTML += appendString;
      }
    }
  }
</script>

还有一些额外的事情可以做,以使其更漂亮,例如在开始时清除innerHtml(可以将其设置为空字符串"")并在附加的字符串之间添加一些分隔符。

英文:

and welcome to StackExchange!

Looking at your code, it looks like you are setting the innerHtml using = which will overwrite the contents with the new value. You will want to replace that with the += (append, so x += 1 is the same as x = x + 1)

<!-- begin snippet: js hide: false console: true babel: false -->

<!-- language: lang-html -->

 &lt;label for = &quot;num&quot;&gt;Enter a number: &lt;/label&gt;
 &lt;input type =&quot;number&quot; id =&quot;num&quot; name =&quot;num&quot;&gt;
 &lt;button id = &quot;btn&quot;&gt;Play&lt;/button&gt;
 &lt;p id = &quot;result&quot;&gt;&lt;/p&gt;

 &lt;script&gt;
  let btn = document.getElementById(&quot;btn&quot;);
  btn.addEventListener(&quot;click&quot;, fizzBuzz);

  function fizzBuzz() {
    let number = document.getElementById(&quot;num&quot;).value;
    for (let i=1; i&lt;=number; i++) {
       let appendString = &quot;&quot;;
       if (i%3 === 0) {
        appendString += &quot;Fizz&quot;;
       }
       if (i%5 === 0) {
        appendString += &quot;Buzz&quot;;
       }
       // !! We changed these two from = to += !!
       if (appendString === &quot;&quot;) {
        document.getElementById(&quot;result&quot;).innerHTML += i;
       } else {
        document.getElementById(&quot;result&quot;).innerHTML += appendString;
       }
     }
  }
    &lt;/script&gt;

<!-- end snippet -->

There will be some additional things you can do to make it prettier, like clearing the innerHtml at the start (you can set to empty string "") and adding some seperator between the appended strings

答案2

得分: 0

不要回答我要翻译的问题。以下是要翻译的内容:

"Instead of saying document.getElementById(&quot;result&quot;).innerHTML = i;, you should instead append it, like so:

if (appendString === &quot;&quot;) {
   document.getElementById(&quot;result&quot;).innerHTML += i;  
} else {
   document.getElementById(&quot;result&quot;).innerHTML += appendString;
}
```"

<details>
<summary>英文:</summary>

Instead of saying `document.getElementById(&quot;result&quot;).innerHTML = i;`, you should instead append it, like so:

```javascript
if (appendString === &quot;&quot;) {
   document.getElementById(&quot;result&quot;).innerHTML += i;  
} else {
   document.getElementById(&quot;result&quot;).innerHTML += appendString;
}

huangapple
  • 本文由 发表于 2023年7月3日 03:25:05
  • 转载请务必保留本文链接:https://go.coder-hub.com/76600470.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定