How do I make a script for odd and even numbers from 1 to 1000 in Javascript?
I am studying a Javascript book with solved examples, but there is one example without a solution. I would like to know how to do this ...
In javascript (in the browser) I need to write even numbers from 1 to 1000 and after it's finished write odd numbers from 1 to 1000 ... I'm not sure how to add a very small "pause" between the number to write there and how find out if the first cycle is over and start writing odd numbers?
This is how I got 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>
source to share
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, all HTML elements in the loaded HTML document will be overwritten. (As you can see in my example)
source to share
I was unable to pause between counting iterations. The following code instead of your script will give you 0-1000 evens, then the odds, 1 per line.
There is already a discussion on expectations in javascript on the site: What is the equivalent of Java 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>
source to share
I'm not sure how to add a "pause" there between writing numbers and find out if the first cycle has ended and start writing odd numbers
Use a function. It should also be in the book you are reading. Use one for even and one for odd. There should be a chapter on event handling where you use elements (like buttons) to handle clicks. Use this to call functions.
Also, try reading on. document.write*
the features aren't really perfect except for things done during page load. Try using much more sophisticated ways to write to the DOM. Those .. should be in this book.
Then, finally, JavaScript doesn't "pause" or something like sleep
. However, it does have timers, but it works differently than sleep
.
One unrelated note, I assume you are using an old book. "HTML5" only requires the "html5 doctype" and <html>
.
<!doctype html>
<html>
...
source to share
The other solutions posted here are correct, but there is no reason to go through the whole module 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);
source to share
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);
}
source to share