Avatar of brunnock

brunnock's solution

to Diamond in the JavaScript Track

Published at Nov 26 2018 · 0 comments
Test suite


This exercise has changed since this solution was written.

The diamond kata takes as its input a letter, and outputs it in a diamond shape. Given a letter, it prints a diamond starting with 'A', with the supplied letter at the widest point.


  • The first row contains one 'A'.
  • The last row contains one 'A'.
  • All rows, except the first and last, have exactly two identical letters.
  • All rows have as many trailing spaces as leading spaces. (This might be 0).
  • The diamond is horizontally symmetric.
  • The diamond is vertically symmetric.
  • The diamond has a square shape (width equals height).
  • The letters form a diamond shape.
  • The top half has the letters in ascending order.
  • The bottom half has the letters in descending order.
  • The four corners (containing the spaces) are triangles.


In the following examples, spaces are indicated by · characters.

Diamond for letter 'A':


Diamond for letter 'C':


Diamond for letter 'E':



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



Install assignment dependencies:

$ npm install

Making the test suite pass

Execute the tests with:

$ npm 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 xtest to test.


Seb Rose http://claysnow.co.uk/recycling-tests-in-tdd/

Submitting Incomplete Solutions

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


import Diamond from './diamond.js';

describe('Make diamond function', () => {
  const diamond = new Diamond();

  test('test letter A', () => {
    const result = 'A\n';

  xtest('test letter C', () => {
    const result = `${['  A  ',
      ' B B ',
      'C   C',
      ' B B ',
      '  A  '].join('\n')}\n`;

  xtest('test letter E', () => {
    const result = `${['    A    ',
      '   B B   ',
      '  C   C  ',
      ' D     D ',
      'E       E',
      ' D     D ',
      '  C   C  ',
      '   B B   ',
      '    A    '].join('\n')}\n`;
const aNum='A'.charCodeAt(0);
class Diamond {
  makeDiamond(letter) {
    let diamond='';
    let letterNum=letter.charCodeAt(0);
    let width=(letterNum-aNum)*2+1;
    let flakeArr= new Array(width).fill(' ');
    flakeArr[0] = letter;
    flakeArr[width-1] = letter;
    diamond += flakeArr.join('') +"\n";
    for (let x=letterNum-1; x>=aNum; --x) {
      let shift=letterNum-x;
      flakeArr= new Array(width).fill(' ');
      flakeArr[shift] = String.fromCharCode(x);
      flakeArr[width-shift-1] = String.fromCharCode(x);
      let flakeStr = flakeArr.join('') +"\n";
      diamond = flakeStr + diamond + flakeStr;

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?