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