javascript的一个新手问题

来源:百度知道 编辑:UC知道 时间:2024/05/14 17:18:34
<body leftmargin=0 topmargin=0>
<script language=javascript>
for(i=1;i<=5;i++)
{

for(x=i;x<5;x++)
{
document.write(" ")
}
for(j=1;j<=i;j++)
{
document.write("* ")
}

document.write("<br>")
}
</script>
</body>
_______________________________________
上面的代码是个金字塔*,在第二个 for 循环中,为什么是从4到1个空格呢?而不是从1到4呢?
还有为什么不可以用 (" ")而是用(" ")呢?
谁能帮忙解答写。 .谢谢!

空格从四到一是因为for(x=i;x<5;x++)X是从i=1开始的,所以输出4个空格,直到i=5,输入0个空格.
您说的 --- (" ")而是用(" ")呢? --- 有区别吗?
其实您的代码应该为:
<body leftmargin=0 topmargin=0>
<script language=javascript>
for(i=1;i<=5;i++)
{
for(x=i;x<5;x++)
{
document.write(" ")//注:此处应为空格的转义字符& n b s p ; 这个在网页中你是显示不出来的.
}

for(j=1;j<=i;j++)
{
document.write("* ")
}
document.write("<br>")
}
</script>
</body>