With below Javascript code you can easily create diamond pattern up to your desire number.
<html> <head> <!-- http://www.codingforums.com/javascript-programming/242014-making-diamond-number-stars-user-inputs.html --> <title>Diamond Pattern</title> </head> <body> <script type = "text/javascript"> function diamond(n) { n = parseInt(n,10); if (n% 2 == 0 ) { alert( "Odd numbers only!" ); return false; } document.write("<pre>"); // top for(i=1; i<=n; i+=2) { for (s=0; s<(n-i/2-n/2); s++) { // -n/2 positions diamond pattern at left hand edge of page document.write(" "); } for (j=1; j<=i; j++) { document.write("*"); } document.write("<br>"); } // bottom for (i=n-2; i>=0; i=i-2) { for (s=0; s<(n-i/2-n/2); s++) { document.write(" "); } for (j=1; j<=i; j++) { document.write("*"); } document.write("<br>"); } document.write("</pre>"); } diamond(7); </script> </body> </html>
0 Comments