简体   繁体   English

我如何在 html 标签(如段落)中使用 if 语句显示 for 循环的结果

[英]How do i display the outcome of my for loop with if statement in an html tag like a paragraph

So the first for loop is what i had for a default console.log and the second on is the one i tried to do it with, but no good outcome sadly.所以第一个 for 循环是我对默认 console.log 所拥有的,第二个是我试图用它来做的,但遗憾的是没有好的结果。

 for (let i = 1; i <= 100; i++) { if (i % 15 == 0) console.log("FB"); else if (i % 3 == 0) console.log("F"); else if (i % 5 == 0) console.log("B"); else console.log(i); } for (let i = 1; i <= 100; i++) { if (i % 15 == 0) document.getElementById("fizzbuzz").innerHTML = "FB"; else if (i % 3 == 0) document.getElementById("fizzbuzz").innerHTML = "F"; else if (i % 5 == 0) document.getElementById("fizzbuzz").innerHTML = "B"; else document.getElementById("fizzbuzz").innerHTML = i; }
 <div class="container"> <h2 class="text-light">Opdracht 2</h2> <p class="text-light" id="fizzbuzz"></p> </div>

Like @36ve mentioned in his comment on your question, and based on additional informations you gave I guess you should change your code from assigment to string concatenation.就像@36ve在他对您的问题的评论中提到的一样,根据您提供的其他信息,我猜您应该将代码从分配更改为字符串连接。

 for (let i = 1; i <= 100; i++) { if (i % 15 == 0) document.getElementById("fizzbuzz").innerHTML += "FB "; else if (i % 3 == 0) document.getElementById("fizzbuzz").innerHTML += "F "; else if (i % 5 == 0) document.getElementById("fizzbuzz").innerHTML += "B "; else document.getElementById("fizzbuzz").innerHTML += `${i} `; }
 <div class="container"> <h2 class="text-light">Opdracht 2</h2> <p class="text-light" id="fizzbuzz"></p> </div>

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

相关问题 如何在高图中显示for循环的结果 - How can I display the outcome of the for loop in highcharts 如何隐藏此段落标签? - How do I hide this paragraph tag? 如何从 javascript 数组中获取项目并将其传输到段落标记中的 html? - How do I take an item from an javascript array and transfer it to html in a paragraph tag? 如何在 HTML 中制作可编辑段落? - How do I make an editable paragraph in HTML? 我正在尝试在 HTML 中显示一段文本,但我需要将其创建为 JSON object 并使用 AJAX 阅读它。 我的代码不工作 - I am trying to display a paragraph of text in HTML but I need to create it as a JSON object and read it with AJAX to do so. My code is not working 我如何导入带有 JavaScript 的 xml 标签以在 html 中显示图像 - How do i import a xml tag with JavaScript to display an image in html 我如何从php的while循环中使用的html标记中获取单个文本值以显示数据库中的数据? - how do i fetch a single text value from html tag used in while loop of php to display data from database? 如何使用HTML5 canvas标签显示视频 - How do I display a video using HTML5 canvas tag 如何基于if语句显示某些html? - How do I display certain html based on if statement? 如何通过JQuery向段落标签添加文本? - How do I add text to a paragraph tag via JQuery?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM