To achieve the desired outcome, the task is to create a function called drawACross
. This function should generate a cross shape consisting of 'x'
characters on a square grid. The size and height of the grid are determined by a single input parameter, n
. Any empty spaces in the grid should be represented by space characters (" "
).
The arms of the cross must intersect at a single central 'x'
character, starting from the corners of the grid. If the value of n
is even, the function should return
"Centered cross not possible!"
.
If n < 3
, the function should output
"Not possible to draw cross for grids less than 3x3!"
.
Below is an example code snippet that attempts to implement this functionality:
function drawACross(n) {
if (n % 2 === 0) {
console.log("Centered cross not possible!")
} else if (n < 3) {
console.log("Not possible to draw cross for grids less than 3x3!")
} else {
for (let i = 0; i < n; i++) {
let arr = new Array(n)
let y = arr.fill("", 0, arr.length)
y.splice(arr.length - (i + 1), 1, "x")
y.splice(i, 1, "x")
console.log(y.join(" "))
}
}
}
drawACross(5);