15,964,792 members
1.00/5 (1 vote)
See more:
Return type should be a object.

drowPattern(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"]]`

Answer should be valid for any given input.

What I have tried:

```function drowPattern(x) {
return false;
}

drowPattern(5);
// Create an empty grid
const grid = [];
// Calculate the middle of the grid
let middle = left = right = Math.floor(X / 2); // works because arrays are 0 based
// Loop through each row of the grid
for (let i = 0; i < X; i++) {
// Create an empty row
const row = [];
// Loop through each column of the row
let j = 0;
for (; j < left; j++)   row.push("o"); // from 0 to left
for (; j <= right; j++) row.push("x"); // from where we are now to right
for (; j < X; j++)      row.push("o"); // from where we are now to end
// Add the row to the grid
grid.push(row.join('')); // join the items
// count up or down
left += i<middle ? -1 : 1; // how far down? if less than the middle move left
right += i<middle ? 1 : -1; // how far down? if less than the middle move right
}
// Return an object with the grid property
return { grid: grid.join('\n') };
}

console.log(drawPattern(5));
console.log(drawPattern(7));```
Posted
Updated 25-Feb-23 1:44am
v2
OriginalGriff 25-Feb-23 7:45am
And?
What have you tried?
Where are you stuck?
What help do you need?

Use the "Improve question" widget to edit your question and provide better information.