简单的Javascript圣诞树

时间:2016-10-13 08:33:33

标签: javascript

我创造了圣诞树的一半,但在这里我被封锁了。有人请帮助我理解如何做左侧。

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

5 个答案:

答案 0 :(得分:2)

&#13;
&#13;
<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>
&#13;
&#13;
&#13;

来源:http://codeforwin.org/2015/07/equilateral-triangle-star-pattern-program-in-c.html

我来自C语言。

答案 1 :(得分:2)

我为此问题编写了以下代码。
我还添加了一个漂亮的额外的圣诞树装饰品:-)

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**     
   *****⛣**⛣  
  *****⌾****⏣ 
 **◍*◍********
      ****
      ****

答案 2 :(得分:2)

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

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() 一起工作不是最佳的,因为数学有点难看,但无论如何,只是为了好玩 =)...

答案 3 :(得分:1)

关键字认为

&#13;
&#13;
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>");
}
&#13;
&#13;
&#13;

约束:x = 5开始只看起来很好

我的原始代码

答案 4 :(得分:0)

简单的圣诞树功能:

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;
}