The new forums will be named Coin Return (based on the most recent vote)! You can check on the status and timeline of the transition to the new forums here.
The Guiding Principles and New Rules document is now in effect.

javascript loops

valtzyvaltzy Registered User regular
edited December 2008 in Help / Advice Forum
Help!

I have the following javascript program

<html>
<head><title>Multiplication Table</title></head>
<body><center><h1><u>Multiplication Tables</h1> (Positive/Negative numbers)</u></center>
<p>
<center><h1>Positive Numbers</h1></center>
<script language="JavaScript">
for(x=1;x<=3;x++)
for(y=1;y<=3;y++)
{document.writeln("<br>",x,"*",y,"=",y*x)}
document.writeln("<hr>")
document.writeln("<center><h1>Negative Numbers</h1></center>")
for(x=1;x<=3;x++)
for(y=1;y<=3;y++)
{document.writeln("<br>",-x,"*",y,"=",y*-x,"<hr>")}

</script>

I need to insert horizontal rules (<hr>) in between blocks of three.

1*1=1
1*2=2
1*3=3
2*1=2
2*2=4
2*3=6
3*1=3
3*2=6
3*3=9

the loop puts it after every line though. What am I doing wrong?

valtzy on

Posts

  • Thor1590Thor1590 Registered User regular
    edited December 2008
    valtzy wrote: »
    Help!

    I have the following javascript program

    <html>
    <head><title>Multiplication Table</title></head>
    <body><center><h1><u>Multiplication Tables</h1> (Positive/Negative numbers)</u></center>
    <p>
    <center><h1>Positive Numbers</h1></center>
    <script language="JavaScript">
    for(x=1;x<=3;x++)
    for(y=1;y<=3;y++)
    {document.writeln("<br>",x,"*",y,"=",y*x)}
    document.writeln("<hr>")
    document.writeln("<center><h1>Negative Numbers</h1></center>")
    for(x=1;x<=3;x++)
    for(y=1;y<=3;y++)
    {document.writeln("<br>",-x,"*",y,"=",y*-x,"<hr>")}

    </script>

    I need to insert horizontal rules (<hr>) in between blocks of three.

    1*1=1
    1*2=2
    1*3=3
    2*1=2
    2*2=4
    2*3=6
    3*1=3
    3*2=6
    3*3=9

    the loop puts it after every line though. What am I doing wrong?

    Edit edit: I see, now. Listen to the guy below me.

    Thor1590 on
  • Bigtoy_JBigtoy_J Registered User regular
    edited December 2008
    Something else to consider. You did not "scope" your outer 'for x' loops.
    <html>
    <head><title>Multiplication Table</title></head>
    <body><center><h1><u>Multiplication Tables</h1> (Positive/Negative numbers)</u></center>
    <p>
    <center><h1>Positive Numbers</h1></center>
    <script language="JavaScript">
    for(x=1;x<=3;x++)
    {
    for(y=1;y<=3;y++)
    {document.writeln("<br>",x,"*",y,"=",y*x)}
    document.writeln("<hr>")
    }
    document.writeln("<center><h1>Negative Numbers</h1></center>")
    for(x=1;x<=3;x++)
    {
    for(y=1;y<=3;y++)
    {document.writeln("<br>",-x,"*",y,"=",y*-x)}
    document.writeln("<hr>")
    }
    </script>
    

    You will see that each for loop contains braces to indicate the scope for the command. If you do not provide braces then the "next line" is the only line that has scope for that loop. A good "rule of thumb" is to always provide matching braces for loops (even if you have only one line to execute). It does not add anything to the overhead and potentially makes the code easier to read.

    I would also suggest a style change, to make it easier to see what code is in what loop (again a personal preference.. You do not HAVE to do this).
    <html>
    <head><title>Multiplication Table</title></head>
    <body><center><h1><u>Multiplication Tables</h1> (Positive/Negative numbers)</u></center>
    <p>
    <center><h1>Positive Numbers</h1></center>
    <script language="JavaScript">
    for(x=1;x<=3;x++)
    {
        for(y=1;y<=3;y++)
        {
            document.writeln("<br>",x,"*",y,"=",y*x)
        }
        document.writeln("<hr>")
    }
    
    document.writeln("<center><h1>Negative Numbers</h1></center>")
    for(x=1;x<=3;x++)
    {
        for(y=1;y<=3;y++)
        {
            document.writeln("<br>",-x,"*",y,"=",y*-x)
        }
        document.writeln("<hr>")
    }
    </script>
    

    Bigtoy_J on
    Love George Bernard Shaw quotations.

    Also, I can count to "boat".
Sign In or Register to comment.