212-32123-4321234-543212345三角形javascript

9rygscc1  于 2021-09-13  发布在  Java
关注(0)|答案(1)|浏览(324)

如何在控制台中制作三角形javascript打印1-212-32123-4321234-543212345?我一点也不成功

var six = "";
for(i=1; i<=6; i++){
    for(j=1; j<=6-i; j++)
    {
        console.log(" ");
    }

    for(j=-i; j<=i; j++)
    {
        if(j==0||j==1)
        {
            continue;
        }
        console.log("%d", Math.abs(j));
    }

    console.log("\n");

}

我需要这样的东西

czq61nw1

czq61nw11#

您可以这样做:

var six = "1"; // do like you already make the first round
for(i=2; i<=6; i++){ // start at 2
   six = i + six + i; // add number at begin and end of string
}

在这里,您可以自己测试它:

var six = "1"; // do like you already make the first round
for(i=2; i<=6; i++){ // start at 2
   document.getElementById("test").innerHTML += six + "<br>";
   six = i + six + i; // add number at begin and end of string
}
<p id="test"></p>

相关问题