简单的Javascript圣诞树

insrf1ej  于 2022-12-17  发布在  Java
关注(0)|答案(8)|浏览(128)

我创造了一半的圣诞树,但在这里我被挡住了。有人请帮助我了解如何做左边太多。

for (var i = 0; i < 8; i++) {
  for (var j = 0; j <= i; j++) {
     document.write("^"); 
  }
  document.write("<br>");
}
ffvjumwh

ffvjumwh1#

<pre>
<script>
    //Reads number of rows to be printed
    var n = 8;
 
    for(i=1; i<=n; i++)
    { 
        //Prints trailing spaces
        for(j=i; j<n; j++)
        {
            document.write(" ");
        }
 
        //Prints the pyramid pattern
        for(j=1; j<=(2*i-1); j++)
        {
            document.write("*");
        }
 
        document.write("<br>");

        }
</script>
</pre>

来源:http://codeforwin.org/2015/07/equilateral-triangle-star-pattern-program-in-c.html
C到JavaScript的转换。

wlsrxk51

wlsrxk512#

我为这个问题编写了下面的代码。
我还加了一个额外的圣诞树装饰品:-)

import java.util.*;
import java.lang.*;
import java.io.*;

class Ideone
{
    private static Random RND = new Random(System.currentTimeMillis()); // useful for placing balls
    private static char[] BALLS = {'o','⌾','⛣','⏣','◍'}; // symbols being used as balls

    public static void main (String[] args) throws java.lang.Exception
    {
        int w = 27; // width of the tree
        int b = 10; // number of balls in the tree
        String tree = ""; // this will end up containing the tree

        // build tree
        w = ( w % 2 == 1 ) ? w : 13; // check whether width is odd
        for(int i=1;i<=w;i+=2){
            int s = (w - i) / 2;
            tree += repeat(' ', s) + repeat('*', i) + repeat(' ', s) + "\n";
        }

        // randomly replace some parts by balls
        int i=0;
        while(i < b){
            int j = RND.nextInt(tree.length());
            if(tree.charAt(j) == '*'){
                tree = tree.substring(0, j) + BALLS[RND.nextInt(BALLS.length)] + tree.substring(j+1);
                i++;
            }
        }

        // build trunk
        tree += repeat(' ', (w - 4) / 2) + repeat('*', 4) + "\n" + repeat(' ', (w - 4) / 2) + repeat('*', 4);

        // output
        System.out.println(tree);
    }

    // this function builds a String by repeating a given character a couple of times
    private static String repeat(char c, int l){
        String s = "";
        for(int i=0;i<l;i++)
            s += c;
        return s;
    }
}

输出应如下所示:

⏣        
      ***       
     *o***    
    **⌾*o**     
   *****⛣**⛣  
  *****⌾****⏣ 
 **◍*◍********
      ****
      ****
8yparm6h

8yparm6h3#

关键字是思考

var x = 8;
for (let i = 0; i < x; i++) {
   for (let j=x-1; j>i; j--) {
      document.write("&nbsp&nbsp"); 
   }
   for (let k=0; k<=(i*2); k++) {
      document.write("^"); 
   }
   document.write("<br>");
}
for (let i=0; i<2; i++) {
    for (let j=0; j<(x*2)-3; j++) {
        document.write("&nbsp");
    }
    document.write("^<br>");
}

制约因素:* 只是看起来不错 * 从x = 5开始。

  • 我的原始代码 *
iqxoj9l9

iqxoj9l94#

上面的答案严重依赖于嵌套循环,我曾用“现代”JS发布过另一种方法(当然,仍然使用单个循环,并为Array.from()提供map函数):

function xmas(height) {
    // add 1 more level for the trunk, e.g. height+1
    return Array.from({length: height+1}, (v, i) => {
        return i === height
            // that's for the trunk of the tree
            ? '*'.padStart(Math.round((2 * i)/2), ' ')
            // the actual tree "levels"
            : '*'.repeat(2 * i + 1).padStart(2 * i + height-i, ' ');
    }).join('\n');
}

document.write(`<pre>${xmas(10)}</pre>`);

也许尝试让它与.padStart()一起工作并不是最优的,因为数学变得有点丑陋,但无论如何,只是为了好玩=)...

fdx2calv

fdx2calv5#

简易采油树功能:

function christmasTree(x) {
    if(x < 3) {
        return "";
    }
    let tree = "";
    for(let i = 1; i <= x; i++) {
        for(let j = 1; j <= x + x - 1; j++) {
            if(j <= x - i || j >= x + i) {
                tree += " ";
            } else {
                tree += "*";
            }
        }
        tree += "\n";
    }
    return tree;
}
zxlwwiss

zxlwwiss6#

如果您正在寻找如何在javascript或typescript的函数中执行此操作
循环使用3,1 -行数2 -空格数3 -字符数

function christmas(n) {
   let tree = '';
    for (let i = 1; i <= n; i++) {
     for (let j=0; j <= n-i; j++) {
      tree += ' ';
     }
     for (k = 0; k< (i*2)-1; k++) {
      tree += '*';
     }
     tree += '\n';
    }
     return tree;
   }

     console.log(christmas(3));
soat7uwm

soat7uwm7#

<pre>
<script>
    //Reads number of rows to be printed
    var n = 8;
 
    for(i=1; i<=n; i++)
    { 
        //Prints trailing spaces
        for(j=i; j<n; j++)
        {
            document.write(" ");
        }
 
        //Prints the pyramid pattern
        for(j=1; j<=(2*i-1); j++)
        {
            document.write("*");
        }
 
        document.write("<br>");

        }
</script>
</pre>
cngwdvgl

cngwdvgl8#

我只是在参加一个编程课程时做了一个练习,这里有一个简单的for循环,没有任何嵌套循环

let row = ""
let l = 9

for (let i = 0; i < l; i++) {
  row += " ".repeat(l - i) + "*" + "*".repeat(i * 2) + `\n`;
}
console.log(row);

相关问题