-1
<html>
<head>
<title>DIsplaying triangle /square</title>

<script type="text/javascript">
document.write("<html><body><p style='line-height:100%;>");
var sides=prompt("Enter the number of sides","3/4");
if(sides==4)
{
    var i=0;

    var j=0;
    var z=0;
    while(i<=6)
    {   
        if(i==0||i==6)
        {
            while(j<=6)
            {
                document.write("*");    
                document.write("<span style='visibility:hidden'>*</span>");
                ++j;
            }
            j=0;
            document.write("<br\>");
        }
        else 
        {
            while(z<=6)
            {
                if(z==0||z==6)
                {
                    document.write("*");
                    document.write("<span style='visibility:hidden'>*</span>");
                }
                else
                {
                    document.write("<span style='visibility:hidden'>*</span>");
                    document.write("<span style='visibility:hidden'>*</span>");
                }
                ++z;
            }
            z=0;
            document.write("<br\>");
        }
        ++i;
    }
    document.write("</p></body></html>");
}
else
{
    var i=0;
}
</script>
</head>
</html>

The output when I enter 4 in the prompt box is supposed to be:

* * * * * * *
* * * * * * *
* * * * * * *
* * * * * * *
* * * * * * *
* * * * * * *
* * * * * * *

(Consider it a perfect square. Its tough to draw it here.)

but I get:

** * * * * *
* * * * * * *
* * * * * * *
* * * * * * *
* * * * * * *
* * * * * * *
* * * * * * *

Look at the first line. It has first 2 "*"s together. As per the code it is not supposed to happen. Something is going wrong in the document.write("*") part. When I tried debugging using alert what I found out is that the first time (i.e when i=0) the first document.write("*") gets ignored. But every other time it is considered.

nupadhyaya
  • 1,811
  • 1
  • 12
  • 13

2 Answers2

0

Check this out: http://jsbin.com/efehiv/1/edit

This line looks bad to me:

document.write("<html><body><p style='line-height:100%;>");
Yevgen
  • 1,180
  • 3
  • 14
  • 26
  • If i remove that line , the spacing between the lines changes and it does not give a perfect square. Thats y i put that line and made line-height:100% which is 100% of the font size(i.e equal to font size..) – nupadhyaya Feb 18 '13 at 13:50
0

Nikhil, you haven't closed your style...begins with single quote and ends with semicolon. Rewrite to

document.write("<html><body><p style=\"line-height:100%;\">");

If you feel the paragraph tag is necessary, that's how you can retain it, but it is not necessary. It is currently messing up the rest of your page though.