我需要使用“for循环”。在javascript中:写出200到500之间的9的倍数。 [英] I need to use a "for loop" in javascript to: write out the multiples of 9 between 200 and 500.

查看:167
本文介绍了我需要使用“for循环”。在javascript中:写出200到500之间的9的倍数。的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

使用for循环我需要在200和500之间写出9的倍数。



我尝试过:



我的代码到目前为止(不工作)



Using a "for loop" I need to Write out the multiples of 9 between 200 and 500.

What I have tried:

My code so far (not working)

<DOCTYPE html>




     <meta charset="utf-8"/>
	 <title>Write out the multiples of 9 between 200 and 500. 
	 
	    
	     > for(i = 200; i &lt;= 500; i++)
         if(i % 9 === 0){
                console.log(i);
         }
  {

推荐答案

页面是空白的,因为你没有创建任何html,如果你想要的话浏览器显示您需要创建html的内容。你正在做的是使用console.log输出到开发工具中的浏览器调试器(通常按f12可用)。



相反,你需要创建一个您想要结果的页面上的div



The page is blank because you're not creating any html, if you want the browser to show something you need to create html. What you're doing is using console.log which outputs to the browsers debugger in the dev tools (usually available by pressing f12).

Instead you need to create a div on the page where you want the results to be

<div id="results">
</div>





接下来使用getElemenetById获取此元素。





Next use getElemenetById to get this element.

var results = document.getElemenetById('results');





现在在你的循环你需要创建一个p元素,或另一个div元素,将数字附加到该元素,然后将该元素添加到结果元素。



HTML DOM createElement()方法 [ ^ ]


这对我来说很好用(顺便说一下,我对javascript有零知识。一起抛出并在https://www.w3schools.com/的帮助下粘合在一起:



This works fine for me (btw, I have Zero knowledge with javascript. Thrown together and glued together with the help of https://www.w3schools.com/):

<!DOCTYPE html>
<html>
<body>

<h2>My First Web Page</h2>
<p>My First Paragraph.</p>

<p id="demo"></p>

<script>
for(i = 200; i <= 500; i++)
{
 if(i % 9 === 0){
 console.log(i);
 }
}
</script>

</body>
</html> 





请记住



Keep in mind

console.log

仅在调试时看到的输出。



顺便说一句。我现在看到破碎代码的问题来自cp格式化更多;)

Output you see only while Debugging.

Btw. I see now the Problems of "broken" code Comes more from cp formatting ;)


这篇关于我需要使用“for循环”。在javascript中:写出200到500之间的9的倍数。的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆