I want the pattern printing using Javascript -
for given number n, print grid shown below using javascript, n positive integer greater 2.
example output n=3
1 1 1
1 0 1
1 1 1
example output n=4
1 1 1 1
1 0 0 1
1 0 0 1
1 1 1 1
function printmatrix(n) { (var = 0; i<n; i++) { var x = ""; for (var j = 0; j <n; j++) { if (i == 0 || == (n-1)) { x += "1"; } else { if (j == 0 || j == (n-1) ) { x +="1"; } else { x += "0"; } } } $("#result").append(x + "<br>"); } } $("#btn").click(function() { $("#result").empty(); printmatrix($("#index").val()); })
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> <input type='number' id='index'> <input type='button' value='print' id='btn'> <div id="result"> </div>
even if didn't try anything, there working solution :
function printmatrix(n) { (var = 0; i<n; i++) { var x = ""; (var j = 0; j <n; j++) { if (i == 0 || == (n-1)) { x += "1"; } else { if (j == 0 || j == (n-1) ) { x +="1"; } else { x += "0"; } } } $("#result").append(x + "<br>"); } } printmatrix(5);
you can improve easily
here jsfiddle
Comments
Post a Comment