Skip to content

Commit 46b8e1a

Browse files
Add new exercise: resistor-color-trio (#725)
* Add resistor-color-trio exercise * Add resistor-color-trio as side exercise * Clean up solution * Fix error throwing test * Unlock the resistor-color-trio by bob
1 parent 17380fd commit 46b8e1a

File tree

8 files changed

+275
-1
lines changed

8 files changed

+275
-1
lines changed

config.json

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -800,6 +800,17 @@
800800
"strings"
801801
]
802802
},
803+
{
804+
"slug": "resistor-color-trio",
805+
"uuid": "7170d6a3-a32f-44d7-b82e-524485c58aaf",
806+
"core": false,
807+
"unlocked_by": "bob",
808+
"difficulty": 5,
809+
"topics": [
810+
"conditionals",
811+
"loops"
812+
]
813+
},
803814
{
804815
"slug": "say",
805816
"uuid": "12989bb3-c593-4f68-bea4-e2c5b76bc3c0",
@@ -1408,4 +1419,4 @@
14081419
"deprecated": true
14091420
}
14101421
]
1411-
}
1422+
}
Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
{
2+
"root": true,
3+
"parser": "babel-eslint",
4+
"parserOptions": {
5+
"ecmaVersion": 7,
6+
"sourceType": "module"
7+
},
8+
"env": {
9+
"es6": true,
10+
"node": true,
11+
"jest": true
12+
},
13+
"extends": [
14+
"eslint:recommended",
15+
"plugin:import/errors",
16+
"plugin:import/warnings"
17+
],
18+
"rules": {
19+
"linebreak-style": "off",
20+
21+
"import/extensions": "off",
22+
"import/no-default-export": "off",
23+
"import/no-unresolved": "off",
24+
"import/prefer-default-export": "off"
25+
}
26+
}
Lines changed: 73 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,73 @@
1+
# Resistor Color Trio
2+
3+
If you want to build something using a Raspberry Pi, you'll probably use _resistors_. For this exercise, you need to know only three things about them:
4+
5+
- Each resistor has a resistance value.
6+
- Resistors are small - so small in fact that if you printed the resistance value on them, it would be hard to read.
7+
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. For example, if they printed a brown band (value 1) followed by a green band (value 5), it would translate to the number 15.
8+
- Each band acts as a digit of a 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.
9+
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 3 colors as input, and outputs the correct value, in ohms.
10+
The colors are mapped to the numbers from 0 to 9 in the sequence:
11+
12+
Black - Brown - Red - Orange - Yellow - Green - Blue - Violet - Grey - White
13+
14+
In `resistor-color duo` you decoded the first two colors. For instance: orange-orange got the main value `33`.
15+
The third color stands for how many zeros need to be added to the main value. The main value plus the zeros gives us a value in ohms.
16+
For the exercise it doesn't matter what ohms really are.
17+
For example:
18+
19+
- orange-orange-black would be 33 and no zeros, which becomes 33 ohms.
20+
- orange-orange-red would be 33 and 2 zeros, which becomes 3300 ohms.
21+
- orange-orange-orange would be 33 and 3 zeros, which becomes 33000 ohms.
22+
23+
(If Math is your thing, you may want to think of the zeros as exponents of 10. If Math is not your thing, go with the zeros. It really is the same thing, just in plain English instead of Math lingo.)
24+
25+
This exercise is about translating the colors into a label:
26+
27+
> "... ohms"
28+
29+
So an input of `"orange", "orange", "black"` should return:
30+
31+
> "33 ohms"
32+
33+
When we get more than a thousand ohms, we say "kiloohms". That's similar to saying "kilometer" for 1000 meters, and "kilograms" for 1000 grams.
34+
So an input of `"orange", "orange", "orange"` should return:
35+
36+
> "33 kiloohms"
37+
38+
## Setup
39+
40+
Go through the setup instructions for Javascript to install the necessary
41+
dependencies:
42+
43+
[https://exercism.io/tracks/javascript/installation](https://exercism.io/tracks/javascript/installation)
44+
45+
## Requirements
46+
47+
Install assignment dependencies:
48+
49+
```bash
50+
$ npm install
51+
```
52+
53+
## Making the test suite pass
54+
55+
Execute the tests with:
56+
57+
```bash
58+
$ npm test
59+
```
60+
61+
In the test suites all tests but the first have been skipped.
62+
63+
Once you get a test passing, you can enable the next one by changing `xtest` to
64+
`test`.
65+
66+
## Source
67+
68+
Maud de Vries, Erik Schierboom [https://github.com/exercism/problem-specifications/issues/1549](https://github.com/exercism/problem-specifications/issues/1549)
69+
70+
## Submitting Incomplete Solutions
71+
72+
It's possible to submit an incomplete solution so you can see how others have
73+
completed the exercise.
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
module.exports = {
2+
presets: [
3+
[
4+
'@babel/env',
5+
{
6+
targets: {
7+
node: 'current',
8+
},
9+
useBuiltIns: false,
10+
},
11+
12+
],
13+
],
14+
};
Lines changed: 62 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,62 @@
1+
const COLORS = [
2+
'black', 'brown', 'red', 'orange', 'yellow', 'green',
3+
'blue', 'violet', 'grey', 'white',
4+
]
5+
6+
const ONE_KILOOHM = 1000
7+
8+
class ArgumentError extends Error {}
9+
10+
export class ResistorColorTrio {
11+
constructor([tens, ones, zeros]) {
12+
this.tens = tens
13+
this.ones = ones
14+
this.zeros = zeros
15+
}
16+
17+
get value() {
18+
if (!this.isValid) {
19+
throw new ArgumentError('invalid color')
20+
}
21+
22+
return this.significants() * this.multiplier()
23+
}
24+
25+
get label() {
26+
return `Resistor value: ${this}`
27+
}
28+
29+
get isValid() {
30+
return COLORS.indexOf(this.tens) > -1
31+
&& COLORS.indexOf(this.ones) > -1
32+
&& COLORS.indexOf(this.zeros) > -1
33+
}
34+
35+
toString() {
36+
const value = this.value
37+
return value < ONE_KILOOHM
38+
? `${value} ohms`
39+
: `${Math.floor(value / ONE_KILOOHM)} kiloohms`
40+
}
41+
42+
/**
43+
* @private
44+
*/
45+
significants() {
46+
return this.colorCode(this.tens) * 10 + this.colorCode(this.ones)
47+
}
48+
49+
/**
50+
* @private
51+
*/
52+
multiplier() {
53+
return Math.pow(10, this.colorCode(this.zeros))
54+
}
55+
56+
/**
57+
* @private
58+
*/
59+
colorCode(color) {
60+
return COLORS.indexOf(color)
61+
}
62+
}
Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
{
2+
"name": "exercism-javascript",
3+
"version": "1.1.0",
4+
"description": "Exercism exercises in Javascript.",
5+
"author": "Katrina Owen",
6+
"private": true,
7+
"repository": {
8+
"type": "git",
9+
"url": "https://github.com/exercism/javascript"
10+
},
11+
"devDependencies": {
12+
"@babel/cli": "^7.5.5",
13+
"@babel/core": "^7.5.5",
14+
"@babel/preset-env": "^7.5.5",
15+
"@types/jest": "^24.0.16",
16+
"@types/node": "^12.6.8",
17+
"babel-eslint": "^10.0.2",
18+
"babel-jest": "^24.8.0",
19+
"eslint": "^6.1.0",
20+
"eslint-plugin-import": "^2.18.2",
21+
"jest": "^24.8.0"
22+
},
23+
"jest": {
24+
"modulePathIgnorePatterns": [
25+
"package.json"
26+
]
27+
},
28+
"scripts": {
29+
"test": "jest --no-cache ./*",
30+
"watch": "jest --no-cache --watch ./*",
31+
"lint": "eslint .",
32+
"lint-test": "eslint . && jest --no-cache ./* "
33+
},
34+
"license": "MIT",
35+
"dependencies": {}
36+
}
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
//
2+
// This is only a SKELETON file for the 'Resistor Color Duo' exercise. It's been provided as a
3+
// convenience to get you started writing code faster.
4+
//
5+
6+
export class ResistorColorTrio {
7+
constructor() {
8+
throw new Error("Remove this statement and implement this function");
9+
}
10+
11+
label() {
12+
throw new Error("Remove this statement and implement this function");
13+
}
14+
}
Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
import { ResistorColorTrio } from './resistor-color-trio.js';
2+
3+
function makeLabel({ value, unit }) {
4+
return `Resistor value: ${value} ${unit}`
5+
}
6+
7+
describe('Resistor Color Trio', () => {
8+
test('Orange and orange and black', () => {
9+
expect(new ResistorColorTrio(["orange", "orange", "black"]).label)
10+
.toEqual(makeLabel({ value: 33, unit: "ohms" }));
11+
});
12+
13+
xtest('Blue and grey and brown', () => {
14+
expect(new ResistorColorTrio(["blue", "grey", "brown"]).label)
15+
.toEqual(makeLabel({ value: 680, unit: "ohms" }));
16+
});
17+
18+
xtest('Red and black and red', () => {
19+
expect(new ResistorColorTrio(["red", "black", "red"]).label)
20+
.toEqual(makeLabel({ value: 2, unit: "kiloohms" }));
21+
});
22+
23+
xtest('Green and brown and orange', () => {
24+
expect(new ResistorColorTrio(["green", "brown", "orange"]).label)
25+
.toEqual(makeLabel({ value: 51, unit: "kiloohms" }));
26+
});
27+
28+
xtest('Yellow and violet and yellow', () => {
29+
expect(new ResistorColorTrio(["yellow", "violet", "yellow"]).label)
30+
.toEqual(makeLabel({ value: 470, unit: "kiloohms" }));
31+
});
32+
33+
// optional: error
34+
xtest('Invalid color', () => {
35+
expect(() => new ResistorColorTrio(["yellow", "purple", "black"]).label)
36+
.toThrowError(/invalid color/);
37+
});
38+
});

0 commit comments

Comments
 (0)