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