Avatar of calebpollman

calebpollman's solution

to Grains in the TypeScript Track

Published at Sep 22 2019 · 0 comments
Test suite

Calculate the number of grains of wheat on a chessboard given that the number on each square doubles.

There once was a wise servant who saved the life of a prince. The king promised to pay whatever the servant could dream up. Knowing that the king loved chess, the servant told the king he would like to have grains of wheat. One grain on the first square of a chess board, with the number of grains doubling on each successive square.

There are 64 squares on a chessboard (where square 1 has one grain, square 2 has two grains, and so on).

Write code that shows:

  • how many grains were on a given square, and
  • the total number of grains on the chessboard

For bonus points

Did you get the tests passing and the code clean? If you want to, these are some additional things you could try:

  • Optimize for speed.
  • Optimize for readability.

Then please share your thoughts in a comment on the submission. Did this experiment make the code better? Worse? Did you learn anything from it?


Go through the setup instructions for TypeScript to install the necessary dependencies:



Install assignment dependencies:

$ yarn install

Making the test suite pass

Execute the tests with:

$ yarn test

In the test suites all tests but the first have been skipped.

Once you get a test passing, you can enable the next one by changing xit to it.


JavaRanch Cattle Drive, exercise 6 http://www.javaranch.com/grains.jsp

Submitting Incomplete Solutions

It's possible to submit an incomplete solution so you can see how others have completed the exercise.


import Grains from "./grains"

describe("returns the number of grains on the square", () => {
  it("1", () => {
    const expected = 1

  xit("2", () => {
    const expected = 2

  xit("3", () => {
    const expected = 4

  xit("4", () => {
    const expected = 8

  xit("16", () => {
    const expected = 32768

  xit("32", () => {
    const expected = 2147483648

  xit("64", () => {
    const expected = 9223372036854775808

  xit("square 0 raises an exception", () => {
    expect(() => Grains.square(0)).toThrow()

  xit("negative square raises an exception", () => {
    expect(() => Grains.square(-1)).toThrow()

  xit("square greater than 64 raises an exception", () => {
    expect(() => Grains.square(65)).toThrow()

describe("returns the total number of grains on the board", () => {
  xit("total", () => {
    const expected = 18446744073709551615
class Grains {
  static square(position: number): number {
    if (position <= 0 || position > 64) {
      throw new Error('bad grains bro');
    let accumulatedGrains: number = 1;
    for (let i = 1; i < position; i++) {
      accumulatedGrains *= 2;
    return accumulatedGrains;

  static total(): number {
    return Grains.square(64) * 2;

export default Grains;

Community comments

Find this solution interesting? Ask the author a question to learn more.

What can you learn from this solution?

A huge amount can be learned from reading other people’s code. This is why we wanted to give exercism users the option of making their solutions public.

Here are some questions to help you reflect on this solution and learn the most from it.

  • What compromises have been made?
  • Are there new concepts here that you could read more about to improve your understanding?