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));
var
This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)