Course outline
Course / The terminal and the language

Build a times-table grid

Lab

How to fetch and submit this lab

Once: install the tool and connect your account.

curl -fsSL https://gonomicon.com/install.sh | sh
gonomicon login

Each 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 submit

Go installed? gonomicon test runs the visible test on your computer first. Submitting also runs hidden checks on our server.

Goal 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. Grid has a // TODO comment and a return line you will change.
  • Use an outer for loop counting rows from 1 to Size, and an inner for loop counting columns from 1 to Size.
  • 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.
  • Size can be 1, giving a single cell followed by a newline. Size can also be 0 or negative, in which case the loops never run and the result is "".
  • Do not change var Size int or the Grid function signature. fmt is 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

  1. 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.
  2. 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.
  3. result := "" for row := 1; row <= Size; row++ { for col := 1; col <= Size; col++ { result += fmt.Sprintf("%4d", row*col) } result += "\n" } return result

Something wrong with this lesson? Report a problem