+ 2

How to make a Diamond pattern using JavaScript

18th Nov 2016, 1:48 PM
Fawad Here
Fawad Here - avatar
2 Respuestas
+ 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("&nbsp");     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("&nbsp"); s++; for(c=1;c<=2*(n-i)-1;c++) document.write("*"); document.write("<br>"); }
18th Nov 2016, 4:55 PM
Aditya kumar pandey
Aditya kumar pandey - avatar
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("&nbsp&nbsp"); } 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>
31st Jul 2018, 5:47 PM
Dev Thakkar
Dev Thakkar - avatar