For example:
Give a JavaScript function that will output 0 and 1 tokens to render a rectangle on a 2d array.
Example output:
0 0 0 0 0 0
0 0 0 0 0 0
0 0 1 1 1 0
0 0 1 1 1 0
0 0 1 1 1 0
It responded:
function generateRectangle(rows, cols, rectStartRow, rectStartCol, rectHeight, rectWidth) { // Create a 2D array filled with 0s let array = Array.from({ length: rows }, () => Array(cols).fill(0));
// Fill the rectangle area with 1s
for (let i = rectStartRow; i < rectStartRow + rectHeight; i++) {
for (let j = rectStartCol; j < rectStartCol + rectWidth; j++) {
array[i][j] = 1;
}
}
return array;
}function printArray(array) { array.forEach(row => { console.log(row.join(' ')); }); }
// Example usage: let rows = 5; let cols = 6; let rectStartRow = 2; let rectStartCol = 2; let rectHeight = 3; let rectWidth = 3;
let rectangleArray = generateRectangle(rows, cols, rectStartRow, rectStartCol, rectHeight, rectWidth); printArray(rectangleArray);
------ And then I asked for a triangle instead, but still with square output example -----
function generateTriangle() { const size = 10; const field = Array.from({ length: size }, () => Array(size).fill(0));
for (let i = 0; i < size; i++) {
for (let j = 0; j <= i; j++) {
field[size - 1 - i][j] = 1;
}
}
return field;
}function printField(field) { for (let row of field) { console.log(row.join(' ')); } }
const triangleField = generateTriangle(); printField(triangleField);
0 0 0 0 0 0 0 0 0 1
0 0 0 0 0 0 0 0 1 1
0 0 0 0 0 0 0 1 1 1
0 0 0 0 0 0 1 1 1 1
0 0 0 0 0 1 1 1 1 1
0 0 0 0 1 1 1 1 1 1
0 0 0 1 1 1 1 1 1 1
0 0 1 1 1 1 1 1 1 1
0 1 1 1 1 1 1 1 1 1
1 1 1 1 1 1 1 1 1 1
----
Then I would execute the function programmatically to generate these tokens.