+ 2
How to make a Diamond pattern using JavaScript
2 odpowiedzi
+ 3
var n = prompt("Please enter number of rows");
n=eval(n);
var s=n-1;
for(var i=1;i<=n;i++)
{
for (var c = 1;c<=s;c++)
document.write(" ");
s--;
for(c=1;c<=2*i-1;c++)
document.write("*");
document.write("<br>");
}
s=1;
for(i=1;i<=n-1;i++)
{
for(c=1;c<=s;c++)
document.write(" ");
s++;
for(c=1;c<=2*(n-i)-1;c++)
document.write("*");
document.write("<br>");
}
0
<!DOCTYPE html>
<html>
<body>
<label>Enter diag no : </label>
<input type="number" id="n">
<button onclick="main()">Print</button>
<script>
function main() {
var n = document.getElementById("n").value;
var space = n-1;
var ss = 1;
for(let i=1; i<n*2; i++) {
var starsp = +n + +ss;
for(let j=1; j<n*2; j++) {
if (space >= j) {
document.write("  ");
}
else if (j > space && j < starsp) {
document.write("*");
}
}
if (i >= n) {
ss = +ss - 1;
space = +space + 1;
}
else {
ss = +ss + 1;
space = +space - 1;
}
document.write("<br />");
}
}
</script>
</body>
</html>