# Thread: Help with simple control statement

1. Member
Join Date
Nov 2010
Posts
3
Rep Power
0

## Help with simple control statement

I'm trying to write a script that prints out the total numbers of 1 to 100 using "while" statements, i.e., add all numbers from 1 to 100.

This is what I have but it doesn't work. I'm new to this stuff...any help?

<?xml version = "1.0" encoding = "utf-8"?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">

<html xmlns = "http://www.w3.org/1999/xhtml">
<head>
<title>Total of 1 to 100</title>
<script type = "text/javascript">
addNumbers() {
var count = 0;
var result = 0;

while (count < 101) {
result += count;
count++;
}
window.alert("The sum of the numbers from one to one hundred is: " + result);
}
</script>
</head>
<body>
<p>Click Refresh (or Reload) to run the script again</p>
</body>
</html>

2. Member
Join Date
Nov 2010
Posts
3
Rep Power
0
Never mind, I got it. I was way off! Here's the correct script for anyone who care:

<?xml version = "1.0" encoding = "utf-8"?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">

<html xmlns = "http://www.w3.org/1999/xhtml">
<head><title>Sum the Integers from 1 to 100</title>
<script type = "text/javascript">

var sum; // stores the total
var x; //counter control variable

x = 1;
sum = 0;

while ( x <= 100 )
{
sum += x;
++x;
} // end while
document.writeln( "The sum is: " + sum )
</script>
</head><body></body>
</html>

3. This has nothing to do with Java and all to do with JavaScript, an unrelated language.

Locking in a minute or three.

4. Member
Join Date
Nov 2010
Posts
3
Rep Power
0
Thanks for the heads up, I don't know what I'm doing.

#### Posting Permissions

• You may not post new threads
• You may not post replies
• You may not post attachments
• You may not edit your posts
•