const drawPattern = (X) => {
const grid = Array.from({ length: X }, () => Array.from({ length: X }, () => ' '));
const middle = Math.floor(X / 2);
let oLen = middle;
for (let i = 0; i < X; i++) {
const o = Array.from({ length: oLen }, () => 'o');
const x = Array.from({ length: X - 2 * oLen }, () => 'x');
grid[i].splice(0, oLen, ...o);
grid[i].splice(X - oLen, oLen, ...o.reverse());
grid[i].splice(oLen, X - 2 * oLen, ...x);
oLen += i < middle ? -1 : 1;
}
return grid;
};
console.log(drawPattern(5));
in my view this code is wright for the following problem. but my quiz platform it is show wrong. can you tell me what should i change this code.
Construct the following pattern in a X by X grid(Multidimensional array)
X should be an odd number
ooxoo
oxxxo
xxxxx
oxxxo
ooxoo
Example :
Input X = 5 should return
[["o","o","x","o","o"],
["o","x","x","x","o"],
["x","x","x","x","x"],
["o","x","x","x","o"],
["o","o","x","o","o"]]