0% found this document useful (0 votes)
6 views

Exercise 10 Loops

The document discusses for loops in JavaScript and provides examples of using for loops to count by twos, count down from 8 to 1, count down from 9 to 0 by threes, and count powers of two. It includes code snippets and exercises for readers to modify the for loops to produce the different counting patterns.

Uploaded by

Uthara Nair
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
6 views

Exercise 10 Loops

The document discusses for loops in JavaScript and provides examples of using for loops to count by twos, count down from 8 to 1, count down from 9 to 0 by threes, and count powers of two. It includes code snippets and exercises for readers to modify the for loops to produce the different counting patterns.

Uploaded by

Uthara Nair
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 2

Coding for Writers: Basic Programming Exercise 10

Exercise 10: Loops


For this exercise, you’ll create some JavaScript functions with for loops. This one uses quite a bit of
math. Once we get into arrays, we can do some fun things with loops and strings.

Start with this code


Create a new file in the your text editor and paste in this code. Note that the body tag has an attribute
called onload. This tells the code to call a function when the page loads. We’ve set it to the demoLoop
function, which has a for loop in it.

<!DOCTYPE HTML>
<html>
<body onload="demoLoop()">

<p id="resultPar"></p>

<script>
function demoLoop() {
var result = "";
for (var i = 0; i < 10; i++) {
result += "<p>" + i + "</p>";
}
resultPar.innerHTML = result;
}

</script>

</body>
</html>

The demoLoop function has a variable called result, which starts out as an empty string. Every time in
the loop, it adds the value of variable i between two paragraph tags. When the loop is over, there will be
10 paragraph tags, which become the html for the resultPar paragraph.

Save this as loops.html. Open it in your browser, and you should see the numbers 0 through 9.

Count by twos
Now modify the for loop so that instead of adding one each time, it adds two. Hint: use the += shortcut.

Save and refresh the page. Note that the total number of paragraphs is lower. That is because the
conditional is still going to make the loop stop when i < 10.

Count down from 8 to 1


Change the for loop so that i starts at 8 and goes down and stops at 1.

1 © 2016 SDK Bridge


Coding for Writers: Basic Programming Exercise 10

Count down from 9 to 0, by threes


Change the for loop so that i starts at 9 and goes down to 0, counting by threes.

Count power of twos


Change the loop so that i starts at 1, which is 2 to the power of 0. Then each time it loops, it multiplies
itself by 2. (Hint: use *= in the increment section) Stop before it gets over 100.

Take a Look at How I’ve Done It


If you get stuck, you can look at my versions of the code:
http://sdkbridge.com/prog1/Exercise10Answers.pdf.

2 © 2016 SDK Bridge

You might also like