Click here to Skip to main content
15,887,135 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

[["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);
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') };
}

console.log(drawPattern(5));
console.log(drawPattern(7));
Posted
Updated 25-Feb-23 1:44am
v2
Comments
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.

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