赋予钻石形状值的功能

时间:2016-07-19 03:30:40

标签: javascript html css

我无法在不操纵html css或居中的情况下尝试创建钻石形状(添加空间足够聪明)。花了几个小时尝试但仍然没有成功。如果不操纵html或css,这是一项不可能完成的任务吗?任何帮助将不胜感激。感谢。

The current state

以下是代码的功能:

function slope(val){

    document.write('function slope('+val+')<br>');
    doubleit = val*2;

    for(i=0; i<doubleit; i++){
        if (i < val){
            for(j=0; j<i; j++){
                document.write('*');
            }
        }
        if (i >= val){
            for(j=doubleit; j>i; j--){
                document.write('*');
            }
        }

    document.write('<br>');
    }
}

function diamond(val){

    doubleit = val*2;
    document.write('<center>');
    document.write('function diamond('+val+')<br>');
    for(i=0; i<doubleit; i++){
        if (i < val){
            for(j=0; j<i; j++){
                document.write('*');
            }
        }
        if (i >= val){
            for(j=doubleit; j>i; j--){
                document.write('*');
            }
        }

        document.write('<br>');
    }

    document.write('</center>');
}

4 个答案:

答案 0 :(得分:1)

如果你想摆脱<center>标签,你必须:

  • 使用固定宽度字体
  • 做正确的左对齐
  • 在&#39; *&#39;之间插入空格字符

示例:

  *
 * *
* * *
 * *
  *

您可以将其视为Nine-Ball starting position的ASCII版本。

让我们试一试:

&#13;
&#13;
function diamond(val){
  var y, w, shape = '';

  for(y = 0; y < val * 2 - 1; y++) {
    w = y < val ? y : val * 2 - y - 2;
    shape += Array(val - w).join(' ') + Array(w + 1).join('* ') + '*\n';
  }
  document.write('<pre>' + shape + '</pre>');
}

diamond(7);
&#13;
&#13;
&#13;

答案 1 :(得分:0)

for(i=1;i<=5;i++){
  txt = "";
  for(j=i;j<10;j++){
    txt += " ";
  }
  for(k=0;k<(i*2)-1;k++){
    txt += "*";
  }
  console.log(txt);
}
var txt = "";
for(i=5-1;i>=1;i--){
  txt = "";
  for(j=i;j<10;j++){
    txt += " ";
  }
  for(k=0;k<(i*2)-1;k++){
    txt += "*";
  }
  console.log(txt);
}

答案 2 :(得分:0)

我是这样做的。


var width = 11;  
var num = (width+1)/2;  
for (let i = num-1; i >-num; i--) {
    for (let j = num-Math.abs(i); j < num; j++) {
        stars+=' '
    }
    for (let j = 0; j < 2*(num-Math.abs(i))-1; j++) { //2*num-(2*Math.abs(i) +1) simplified to 2*(num-Math.abs(i))-1
        stars+="*";
    }
    stars+="\n";
}
console.log(stars);

答案 3 :(得分:0)

function diamondOfWidth(n){
    var i, diamonds = '*';
    for(i=1; i< 2*n; i++){
        document.write('<pre>' + diamonds + '</pre>');
        diamonds += i<n? "  *": '';
        diamonds = i>=n? diamonds.slice(3): diamonds;
    };
    document.body.style.textAlign = 'center';
    document.body.style.lineHeight = 0.5;
};

function diamondOfHeight(n){
    var i, diamonds = '*';
    for(i=1; i <= n+1; i++){
        document.write('<pre>' + diamonds + '</pre>');
        diamonds += i< n/2? ' * *': '';
        diamonds = i>n/2? diamonds.slice(4): diamonds;
    };
    document.body.style.textAlign = 'center';
    document.body.style.lineHeight = 0.1;
};

diamondOfHeight(7);
diamondOfHeight(6);
diamondOfHeight(5);
diamondOfHeight(4);
diamondOfHeight(3);

diamondOfWidth(7);
diamondOfWidth(6);
diamondOfWidth(5);
diamondOfWidth(4);
diamondOfWidth(3);
<h1> Diamonds </h1>