最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

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

programmeradmin1浏览0评论

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 ...

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?

Here is How I started:

<?xml version="1.0" encoding="utf-8"?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.1//EN" ".dtd">
<html xmlns="" 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>

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 ...

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?

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>
Share Improve this question asked Nov 13, 2014 at 1:56 ByakuganByakugan 9814 gold badges16 silver badges35 bronze badges 3
  • There is no int keyword in JavaScript! Use var keyword. – Ram Commented Nov 13, 2014 at 1:58
  • using document.write is bad practice...why do books still teach it.... – epascarello Commented Nov 13, 2014 at 2:05
  • Probably because a lot of programmers are used to the REPL and printing is one of the most simple ways to provide output. Otherwise, you need to teach the DOM – Charlie Commented Oct 18, 2016 at 2:32
Add a comment  | 

8 Answers 8

Reset to default 5

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.write for testing only. If you execute it, on a loaded HTML document, all HTML elements will be overwritten. ( As you can see in my example )

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.

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

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

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.write for testing purpose

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!

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.

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


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

<!doctype html>
  <html>
    ...

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);
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);

}
 <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>
发布评论

评论列表(0)

  1. 暂无评论