-2

How to get the matrix like values i have given below when only n>=3 using javascript?

 n=3: 
[ (1 1 1)  
  (1 0 1)  
  (1 1 1)
] 

and n=4:
 [(1 1 1 1)
  (1 0 0 1) 
  (1 0 0 1) 
  (1 1 1 1)
 ]

...... and so on. Please give me answer. Thank you

RIYAJ KHAN
  • 15,032
  • 5
  • 31
  • 53
  • Show us something that you have tried – Sam Orozco Jul 14 '16 at 05:23
  • 1
    This was asked the other day: http://stackoverflow.com/questions/38306912/i-want-the-pattern-printing-using-javascript/ (Although that other question was asking about "printing" the grid, not returning an array of arrays - if an array of arrays is even what you mean with that `[()()()]` notation. But if you can achieve one you can likely achieve the other.) – nnnnnn Jul 14 '16 at 05:25
  • Possible duplicate of [How can I get query string values in JavaScript?](http://stackoverflow.com/questions/901115/how-can-i-get-query-string-values-in-javascript) – alexandresaiz Jul 14 '16 at 06:16

1 Answers1

0

Basically you could generate a nested array and fill it with the right value with a check if one index is zero or the last possible index value.

var n = 5,
    array = Array.apply(null, { length: n }).map(function (_, i) {
        return Array.apply(null, { length: n }).map(function (_, j) {
            return +(i === 0 || i + 1 === n || j === 0 || j + 1 === n);
        });
    });

console.log(array);
Nina Scholz
  • 376,160
  • 25
  • 347
  • 392