
beginner question
Hey guys,
I've been sitting here trying to get this simple script to run properly. The first thing is the whole "<" used for the less than 5 times part, its interfering with my <br/>, it acts like its apart of it? Also the sum is supposed to be displayed after the calculation to done. I just had the sum part working, not something magically stopped, ugh.
Its suppose to look like:
1:1
2:3
3:6
4:10
5:15
This is what I've wrote so far, I am a total beginner on this, thanks!
script type="text/javascript">
var N=0;
var sum=0;
for (var times=0; times<5; times++)
{
N=N+1
document.write(sum = N + ":");
document.write(N + "<br/>");
}
</script>

Originally Posted by blankshadow555
Hey guys,
I've been sitting here trying to get this simple script to run properly. The first thing is the whole "<" used for the less than 5 times part, its interfering with my <br/>, it acts like its apart of it? Also the sum is supposed to be displayed after the calculation to done. I just had the sum part working, not something magically stopped, ugh.
Its suppose to look like:
1:1
2:3
3:6
4:10
5:15
This is what I've wrote so far, I am a total beginner on this, thanks!
script type="text/javascript">
var N=0;
var sum=0;
for (var times=0; times<5; times++)
{
N=N+1
document.write(sum = N + ":");
document.write(N + "<br/>");
}
</script>
What is the relationship of the loop count (times) to the N values after each loop.
Right now, you have it setup to just show the value of N after each loop.
More statements in RED below
Code:
<script type="text/javascript">
var N=0;
var sum=0;
for (var times=0; times<5; times++) {
N=N+1
document.write(sum = N + ":"); // this is an equation you are outputting
document.write(N + "<br/>"); // this is just N incremented each loop of the for
}
</script>
BTW: You should enclose your script between [ code] and [ /code] tags (without the spaces)
to make it easier to read, copy, test and retain format for other forum readers.
Without a math relationship, the only way I see to get that output is to use an array.
Code:
<script type="text/javascript">
var arr = [1,3,6,10,15];
var N = 0;
for (var times=0; times<5; times++) {
N = N + 1;
document.write( N + ":" + arr[times] + "<br/>");
}
</script>
or even smaller...
Code:
<script type="text/javascript">
var arr = [1,3,6,10,15];
for (var N=0; N<arr.length; N++) {
document.write( (N+1) + ":" + arr[N] + "<br/>");
}
</script>
Last edited by JMRKER; 08202012 at 08:02 PM.

okay so maybe I've been doing this wrong the whole time, I needed to increase N by 1, until N reaches 5, while summing the total. I have no idea

Originally Posted by blankshadow555
okay so maybe I've been doing this wrong the whole time, I needed to increase N by 1, until N reaches 5, while summing the total. I have no idea
Then you need to analyze this...
Code:
<script type="text/javascript">
var sum = 0;
var N = 0;
while (N < 5) {
N++; sum += N;
document.write( N + ":" + sum + "<br/>");
}
</script>
Make sure you can explain each statement before you turn it in for homework (???)

Hey thank you greatly, this is an online class so instructor help is delayed, I was using the wrong statement? I guess I was trying to use for instead of the while. My brain has been blended over this

Originally Posted by blankshadow555
Hey thank you greatly, this is an online class so instructor help is delayed, I was using the wrong statement? I guess I was trying to use for instead of the while. My brain has been blended over this
For loops and while loops can be made equivalent...
Code:
<script type="text/javascript">
var sum = 0;
for (var N=1; N<=5; N++) {
sum += N;
document.write( N + ":" + sum + "<br/>");
}
</script>
Go back to your original code and note the differences.
Thread Information
Users Browsing this Thread
There are currently 1 users browsing this thread. (0 members and 1 guests)
Posting Permissions
 You may not post new threads
 You may not post replies
 You may not post attachments
 You may not edit your posts

Forum Rules

"
"
