Exercism v3 launches on Sept 1st 2021. Learn more! 🚀🚀🚀
Avatar of WebCu

WebCu's solution

to Resistor Color Duo in the TypeScript Track

Published at Jun 29 2021 · 0 comments
Instructions
Test suite
Solution

If you want to build something using a Raspberry Pi, you'll probably use resistors. For this exercise, you need to know two things about them:

  • Each resistor has a resistance value.
  • Resistors are small - so small in fact that if you printed the resistance value on them, it would be hard to read.

To get around this problem, manufacturers print color-coded bands onto the resistors to denote their resistance values. Each band has a position and a numeric value.

The first 2 bands of a resistor have a simple encoding scheme: each color maps to a single number. For example, if they printed a brown band (value 1) followed by a green band (value 5), it would translate to the number 15.

In this exercise you are going to create a helpful program so that you don't have to remember the values of the bands. The program will take color names as input and output a two digit number, even if the input is more than two colors!

The band colors are encoded as follows:

  • Black: 0
  • Brown: 1
  • Red: 2
  • Orange: 3
  • Yellow: 4
  • Green: 5
  • Blue: 6
  • Violet: 7
  • Grey: 8
  • White: 9

From the example above: brown-green should return 15 brown-green-violet should return 15 too, ignoring the third color.

Setup

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

https://exercism.io/tracks/typescript/installation

Requirements

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.

Source

Maud de Vries, Erik Schierboom https://github.com/exercism/problem-specifications/issues/1464

Submitting Incomplete Solutions

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

resistor-color-duo.test.ts

import { ResistorColor } from './resistor-color-duo'

describe('Resistor Colors', () => {
  it('Brown and black', () => {
    const resistorColor = new ResistorColor(['brown', 'black'])
    expect(resistorColor.value()).toEqual(10)
  })

  xit('Blue and grey', () => {
    const resistorColor = new ResistorColor(['blue', 'grey'])
    expect(resistorColor.value()).toEqual(68)
  })

  xit('Yellow and violet', () => {
    const resistorColor = new ResistorColor(['yellow', 'violet'])
    expect(resistorColor.value()).toEqual(47)
  })

  xit('Orange and orange', () => {
    const resistorColor = new ResistorColor(['orange', 'orange'])
    expect(resistorColor.value()).toEqual(33)
  })

  xit('Ignore additional colors', () => {
    const resistorColor = new ResistorColor(['green', 'brown', 'orange'])
    expect(resistorColor.value()).toEqual(51)
  })

  xit('Throws error when not enough colors', () => {
    expect(() => new ResistorColor(['green'])).toThrowError(
      'At least two colors need to be present'
    )
  })
})
type BandColor = 'black' | 'brown' | 'red' | 'orange' | 'yellow' | 'green' | 'blue' | 'violet' | 'grey' | 'white';
type ResitanceValue = 0 | 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9;

const resistors: Map<BandColor, ResitanceValue> = new Map([
  ['black', 0],
  ['brown', 1],
  ['red', 2],
  ['red', 2],
  ['orange', 3],
  ['yellow', 4],
  ['green', 5],
  ['blue', 6],
  ['violet', 7],
  ['grey', 8],
  ['white', 9],
])

export class ResistorColor {
  private colors: string[]
  
  constructor(colors: string[]) {
    if (colors.length < 2)
      throw new Error('At least two colors need to be present')

    this.colors = colors
  }

  value = (): number => {
    return parseInt(
      this.colors
        .slice(0, 2)
        .reduce((accumulator: string, currentColor: string): string => {
          return accumulator + (resistors.get(currentColor as BandColor)?.toString() ?? '')
        }, '')
    )
  }
}

Community comments

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

WebCu's Reflection

Initially, I tried to solve this exercise using [Maps](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Map) but it was very confusing to me. Then I discover [Index Signatures](https://www.typescriptlang.org/docs/handbook/2/objects.html#index-signatures) and I was finally able to access the keys in an efficient way and enforce the types!