ðŸŽ‰ Exercism Research is now launched. Help Exercism, help science and have some fun at research.exercism.io ðŸŽ‰

# jaeyson's solution

## to Resistor Color in the Elixir Track

Published at Feb 07 2021 · 0 comments
Instructions
Test suite
Solution

Resistors have color coded bands, where each color maps to a number. The first 2 bands of a resistor have a simple encoding scheme: each color maps to a single number.

These 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

Mnemonics map the colors to the numbers, that, when stored as an array, happen to map to their index in the array: Better Be Right Or Your Great Big Values Go Wrong.

## Running tests

Execute the tests with:

``````\$ mix test
``````

### Pending tests

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

Once you get a test passing, you can unskip the next one by commenting out the relevant `@tag :pending` with a `#` symbol.

For example:

``````# @tag :pending
test "shouting" do
assert Bob.hey("WATCH OUT!") == "Whoa, chill out!"
end
``````

Or, you can enable all the tests by commenting out the `ExUnit.configure` line in the test suite.

``````# ExUnit.configure exclude: :pending, trace: true
``````

If you're stuck on something, it may help to look at some of the available resources out there where answers might be found.

## Source

This is an exercise to introduce users to basic programming constructs, just after Hello World. Electronic color code Wikipedia article

## Submitting Incomplete Solutions

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

### resistor_color_test.exs

``````defmodule ResistorColorTest do
use ExUnit.Case

# @tag :pending
test "returns black color code" do
assert ResistorColor.code("black") == 0
end

@tag :pending
test "returns brown color code" do
assert ResistorColor.code("brown") == 1
end

@tag :pending
test "returns red color code" do
assert ResistorColor.code("red") == 2
end

@tag :pending
test "returns orange color code" do
assert ResistorColor.code("orange") == 3
end

@tag :pending
test "returns yellow color code" do
assert ResistorColor.code("yellow") == 4
end

@tag :pending
test "returns green color code" do
assert ResistorColor.code("green") == 5
end

@tag :pending
test "returns blue color code" do
assert ResistorColor.code("blue") == 6
end

@tag :pending
test "returns violet color code" do
assert ResistorColor.code("violet") == 7
end

@tag :pending
test "returns grey color code" do
assert ResistorColor.code("grey") == 8
end

@tag :pending
test "returns white color code" do
assert ResistorColor.code("white") == 9
end

@tag :pending
test "returns all colors" do
colors = [
"black",
"brown",
"red",
"orange",
"yellow",
"green",
"blue",
"violet",
"grey",
"white"
]

assert ResistorColor.colors() == colors
end
end``````

### test_helper.exs

``````ExUnit.start()
ExUnit.configure(exclude: :pending, trace: true)``````

### lib/resistor_color.ex

``````defmodule ResistorColor do
@moduledoc false

@colors [
"black",
"brown",
"red",
"orange",
"yellow",
"green",
"blue",
"violet",
"grey",
"white"
]

@spec colors() :: list(String.t())
def colors, do: @colors

@spec code(String.t()) :: integer()
def code(color),
do: Enum.find_index(@colors, &(&1 === color))
end``````

### test/resistor_color_test.exs

``````defmodule ResistorColorTest do
use ExUnit.Case

# @tag :pending
test "returns black color code" do
assert ResistorColor.code("black") == 0
end

# @tag :pending
test "returns brown color code" do
assert ResistorColor.code("brown") == 1
end

# @tag :pending
test "returns red color code" do
assert ResistorColor.code("red") == 2
end

# @tag :pending
test "returns orange color code" do
assert ResistorColor.code("orange") == 3
end

# @tag :pending
test "returns yellow color code" do
assert ResistorColor.code("yellow") == 4
end

# @tag :pending
test "returns green color code" do
assert ResistorColor.code("green") == 5
end

# @tag :pending
test "returns blue color code" do
assert ResistorColor.code("blue") == 6
end

# @tag :pending
test "returns violet color code" do
assert ResistorColor.code("violet") == 7
end

# @tag :pending
test "returns grey color code" do
assert ResistorColor.code("grey") == 8
end

# @tag :pending
test "returns white color code" do
assert ResistorColor.code("white") == 9
end

# @tag :pending
test "returns all colors" do
colors = [
"black",
"brown",
"red",
"orange",
"yellow",
"green",
"blue",
"violet",
"grey",
"white"
]

assert ResistorColor.colors() == colors
end
end``````