I had a task recently to do an empty triangle based on a random height given by a user.
It had to look like that but the user was the one who could set the height.
My script looks like this now:
var hgh = prompt("Set the triangle's height: ");
document.write("<center><pre>");
for (var i = 1; i <= hgh ; i++) {
var s = "";
for (var j = 1; j <= (2 * hgh - 1); j++) {
if (i != hgh ) {
if (j == hgh + 1 - i || j == hgh - 1 + i) {
s += "X";
}
else {
s += " ";
}
}
else {
s += "X";
}
}
document.write(s);
document.write("<br>");
}
document.write("</pre></center>");
Which part of my script do I have to correct so that it displays the triangle correctly?