Click here to Skip to main content
15,666,313 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
Return type should be a object.

drowPattern(5) should return


Answer should be valid for any given input.

What I have tried:

function drowPattern(x) {
  return false;

function drawPa`your text`ttern(X) {
  // 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') };

Updated 25-Feb-23 1:44am
OriginalGriff 25-Feb-23 7:45am    
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.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900