如何在 Javascript 中为从 1 到 1000 的奇数和偶数编写脚本?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/26900080/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-22 23:30:10  来源:igfitidea点击:

How to do a script for odd and even numbers from 1 to 1000 in Javascript?

javascriptloopsnumbers

提问by Byakugan

I am studying a book of Javascript with solved examples but there is one example without solution. I would like to know how to do it ...

我正在学习一本带有已解决示例的 Javascript 书,但有一个示例没有解决方案。我想知道怎么做...

In javascript (in browser) I should do is write even numbers from 1-1000 and after it is finished write odd numbers from 1-1000 ... I am not sure how to add there very small "pause" between number writing and how to know if first cycle is over and start writing odd numbers?

在javascript(在浏览器中)我应该做的是写1-1000的偶数,完成后写1-1000的奇数......我不知道如何在数字写作之间添加非常小的“暂停”以及如何知道第一个周期是否结束并开始写奇数?

Here is How I started:

这是我如何开始的:

<?xml version="1.0" encoding="utf-8"?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.1//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml11.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en">
 <head>
   <title>Test</title>
 </head>
 <body>
<script type="text/javascript">
/* <![CDATA[ */
var i;
for (i = 0; i < 1000; i++)
if ((i % 2) == 0)
  document.writeln(i);

/* ]]> */
</script>
 </body>
</html>

回答by baao

Give this a try:

试试这个:

 function partA() {
        for (var i = 0; i < 1000; i++){
            if ((i % 2) == 0) document.write(i + ' ');
        }
        window.setTimeout(partB,1000)
    }

    function partB() {
        for (var i = 0; i < 1000; i++){
            if ((i % 2) !== 0) document.write(i + ' ');
        }
    }

    partA();


SIDENOTE:

边注:

Use document.writefor testing only. If you execute it, on a loaded HTML document, all HTML elements will be overwritten. ( As you can see in my example )

使用document.write仅用于测试。如果执行它,在加载的 HTML 文档上,所有 HTML 元素都将被覆盖。(正如您在我的示例中所见)

回答by ianaya89

You should try something like this:

你应该尝试这样的事情:

(function(){

  for (var i = 0; i < 1000; i++){
    if ((i % 2) === 0) document. write(i + ' ');
  }

  for (var i = 0; i < 1000; i++){
    if ((i % 2) !== 0) document. write(i + ' ');
  }
})();

*You should only use document.writefor testing purpose

*您应该仅document.write用于测试目的

回答by Greg Syme

I was not able to make a pause occur between the iterations of counting. The below code, in place of your script, will give you 0-1000 evens, then odds, 1 per line.

我无法在计数迭代之间暂停。下面的代码代替您的脚本,将为您提供 0-1000 个偶数,然后是赔率,每行 1 个。

There is some discussion of waiting in javascript already on the site: What's the equivalent of Java's Thread.sleep() in JavaScript?

网站上已经有一些关于在 javascript 中等待的讨论:JavaScript中 Java 的 Thread.sleep() 等价物是什么?

<script>
for(var mod = 0; mod<2; mod++){
  for (var i = 0; i < 1000; i++)
    if ((i % 2) == mod)
      document.writeln(i+"<br>");
}
</script>

回答by baron

First of all if you want even number from 1 to 1000 your iteration variable should start from 1, not 0. :) To write odd numbers after even ones you can just put another loop behind the first one with if(i%2==1) and it should be fine!

首先,如果你想要从 1 到 1000 的偶数,你的迭代变量应该从 1 开始,而不是 0。:) 要在偶数之后写奇数,你可以用 if(i%2== 1)应该没问题!

回答by Joseph

I am not sure how to add there the "pause" between number writing and how to know if first cycle is over and start writinf odd numbers

我不知道如何在数字写入之间添加“暂停”以及如何知道第一个周期是否结束并开始写入奇数

Use a function. It should also be in that book you're reading. Use one for even and another for odd. There should be a chapter regarding event handling, where you use elements (like buttons) to handle clicks. Use that to call the functions.

使用一个函数。它也应该在你正在阅读的那本书中。一个用于偶数,另一个用于奇数。应该有一章关于事件处理,您可以在其中使用元素(如按钮)来处理点击。使用它来调用函数。

Also, try reading further. document.write*functions aren't really ideal, save for stuff done during the loading of the page. Try using much more advanced ways to write to the DOM. Those.. should be in that book as well.

另外,尝试进一步阅读。document.write*功能不是很理想,除了在页面加载期间完成的工作。尝试使用更高级的方法来写入 DOM。那些……也应该在那本书里。

Then lastly, JavaScript doesn't "pause", nor does it have something like sleep. It has timers though, but it works differently from sleep.

最后,JavaScript 不会“暂停”,也没有类似sleep. 虽然它有计时器,但它的工作方式与sleep.



One an unrelated note, I believe you're using an old book. "HTML5" only requires a the "html5 doctype" and <html>.

一个不相关的笔记,我相信你正在使用一本旧书。“HTML5”只需要“html5 doctype”和<html>.

<!doctype html>
  <html>
    ...

回答by James Irwin

Other solutions posted here are correct, but there's no reason to go through all the modulo work:

此处发布的其他解决方案是正确的,但没有理由进行所有模运算:

function evens () {
    var i;
    for (i = 2; i <= 1000; i++,i++) {
        document.writeln(i + '<br>');
    }
};
function odds () {
    var i;
    for (i = 1; i < 1000; i++,i++) {
        document.writeln(i + '<br>');
    }               
};
evens();
setTimeout(odds, 2000);

回答by nourddine bakour

function setCode() {
    var textbox1 = document.getElementById('textbox1');
    var textbox2 = document.getElementById('textbox2');
    var divResult = document.getElementById('divResult');
    var c = ['azimuth','background','background-attachment',
    'background-color','background-image',
    'background-position','background-repeat',
    'behavior','border','border-bottom',
    'border-bottom-color','border-bottom-style',
    'border-bottom-width','border-collapse',
    'border-color','border-left','border-left-color',
    'border-left-style','border-left-width','border-right',
    'border-right-color','border-right-style',
    'border-right-width','border-spacing','border-style',
    'border-top','border-top-color','border-top-style',];

var code0 = ( function set(C0) {
        return ( C0 +=
    [0, 1, 2, 3, 4, 5, 6, 7, 8, 9]
    [Math.floor(Math.random()*10)])
    && (C0.length == 3) ? C0 : set(C0);
    }
)('');
    textbox1.value =  code0;
    divResult.innerHTML = c[textbox1.value];
    var t;
    t = setTimeout('setCode()', 1000);

}

回答by shutupchigo

 <script type="text/javascript">
    var number = 0;
    while (number <= 1000) {
        if (number % 2 === 0) {
            document.write(number + " is even number <br />");
            number = number + 1;

        } else {
            document.write(number + " is odd number <br/>");
            number = number + 1;
        }
    }
</script>