Course / The terminal and the language
Build a times-table grid
How to fetch and submit this lab
Once: install the tool and connect your account.
curl -fsSL https://gonomicon.com/install.sh | sh
gonomicon loginEach lab: download it, edit the file named in the instructions, then submit.
gonomicon fetch week-1/11-05-times-table-grid
cd 11-05-times-table-grid
gonomicon submitGoal
Build a Size x Size multiplication table as a multi-line string, using nested for loops.
Example
For Size = 3, the grid (shown with · standing in for a space, so you can count them) looks like:
··1··2··3
··2··4··6
··3··6··9
Each number sits in its own 4-character-wide column, right-aligned, so the numbers line up no matter how many digits they have. Every row, including the last one, ends with a newline.
Rules
- Edit
grid.go.Gridhas a// TODOcomment and a return line you will change. - Use an outer
forloop counting rows from1toSize, and an innerforloop counting columns from1toSize. - Each cell's value is
row * col. - Format each cell with
fmt.Sprintf("%4d", row*col)and append it directly to your result string — the 4-character field width is what keeps columns lined up. - After the inner loop finishes for a row, append a newline (
"\n") before moving to the next row. Sizecan be1, giving a single cell followed by a newline.Sizecan also be0or negative, in which case the loops never run and the result is"".- Do not change
var Size intor theGridfunction signature.fmtis already an allowed import.
What you'll practice
Nesting one for loop inside another, and building up a multi-line result with an accumulator.
Run it
gonomicon fetch week-1/11-05-times-table-grid
cd 11-05-times-table-grid
gonomicon test
gonomicon submit
Hints
- Nest two three-component for loops: the outer one counts rows from 1 to Size, the inner one counts columns from 1 to Size. Build up the result string with an accumulator, one cell at a time.
- For each cell, print rowcol right-aligned in a 4-character-wide field with fmt.Sprintf("%4d", rowcol), and append that straight onto result — no separate separator needed, the padding does the lining-up for you. After the inner loop finishes, append a newline to end that row.
- result := "" for row := 1; row <= Size; row++ { for col := 1; col <= Size; col++ { result += fmt.Sprintf("%4d", row*col) } result += "\n" } return result