我需要做一个简单的for
循环,使它最终看起来像这样:
1****** 12***** 123**** 1234*** 12345** 123456* 1234567
到目前为止,我写道:
for (i = 1; i <= 7; ++i) {
document.write(i);
for (j = 1; j < 7; j++) {
if (j <= 7) {
document.write("*");
} else {
document.write(" ");
}
}
}
答案 0 :(得分:2)
你需要另一个变量来保存循环中迭代的数字。
for (i = 1, k = ""; i <= 7; ++i) {
k += i; // concatenating i variable values in k variable
document.write(k);
for (j = i; j < 7; j++) {
document.write("*");
}
document.write(" ");
}
答案 1 :(得分:1)
你快到了那里:
for (i = 1; i <= 7; ++i) {
for (j = 1; j <= 7; j++) {
if (j <= i)
document.write(j);
else
document.write("*");
}
document.write(" ");
}
您的评论代码:
for (i = 1; i <= 7; ++i) { // This for should loop over your blocks
document.write(i); // Doing this will make the first number change for every block.
for (j = 1; j < 7; j++) { // This for should loop over your numbers
if (j <= 7) { // This if is only there for the space? Put it in the other loop!
document.write("*");
} else {
document.write(" ");
}
// Here you should check if you want a * or a number. Not the check to see if you want a space or not.
}
// Space should be here.
}
答案 2 :(得分:0)
你不远,有几项改变应该这样做:
for (i = 1; i <= 7; i++) {
for (j = 1; j <= 7; j++) {
if (j <= i)
document.write(j);
else
document.write("*");
}
document.write(" ");
}
&#13;
答案 3 :(得分:-2)
试试这个: 首先,你有一个从1到7的主循环, 然后你有第二个循环写你的字符串, 所以从1开始,直到我你正在写数字然后添加** 这是jsfiddle
for (i=1;i<=7;i++)
{
for(j=1; j<=i; j++)
document.write(j);
{
while(j<=7)
{
document.write("*");
j++;
}
document.write(" ");
}
}