JessieBear JessieBear - 5 months ago 7
Javascript Question

Javascript code for loop don't show my desired ouput

My code below don't show my desired output.

for (var row=1;row<=8;row++){

for (var i=1;i<=4-(4-row);i++){
document.write("#");
}
document.write("</br>");
}


I want the output like this

enter image description here

Answer

var x = 8;
var y = parseInt(x / 2);

for (var i = 0; i < x; i++) {
  for (var j = 0; j < y - Math.abs(i - y); j++)
    document.write('#');
  document.write('<br/>');
}

Comments