...

View Full Version : How to use a loop to repeat a sequence of numbers?



helpneeded22
09-29-2012, 09:05 PM
I want to use javascript to create an HTML table that consists of 4 columns and a number of rows determined by a inputted value. Each column will have different code that will be associated with it. To do this, I will need to incorporate this all into a loop. I need a formula or other method to basically break the loop sequence numbers into instances of 1 - 4.

i.e.:

Loop Number / Instance Value

1 / 1
2 / 2
3 / 3
4 / 4
5 / 1
6 / 2
7 / 2
8 / 2
9 / 3
etc

Can someone suggest a method for doing this please?

Thanks.

xelawho
09-29-2012, 09:08 PM
I don't see the pattern. What happens after 9?

Philip M
09-30-2012, 10:57 AM
I don't see the pattern or really understand the question either. Does this point the way?



<script type = "text/javascript">

function showvals() {
var num = Number(document.getElementById("rows").value) || 0;
for (var i = 1; i<=num; i++) {
for (var j = 1; j<=4; j++) {
document.write(i*j + "&nbsp&nbsp&nbsp")
}
document.write ("<br><br>");
}
}

</script>

Of course, document.write() is not used in real applications. Just for the purpose of this demo.

Quizmaster: Emmental and Double Gloucester are both types of what?
Contestant: Banks.



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum