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

佐手、 提交于 2021-02-04 19:20:11

问题


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>

回答1:


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 )




回答2:


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>



回答3:


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




回答4:


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!




回答5:


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



回答6:


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



回答7:


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

}



回答8:


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


来源:https://stackoverflow.com/questions/26900080/how-to-do-a-script-for-odd-and-even-numbers-from-1-to-1000-in-javascript

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!