-
-
Notifications
You must be signed in to change notification settings - Fork 637
Add new exercise: resistor-color-trio #725
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
SleeplessByte
merged 5 commits into
exercism:master
from
SleeplessByte:exercise/resistor-color-trio
Oct 1, 2019
Merged
Changes from all commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
cbfd1de
Add resistor-color-trio exercise
SleeplessByte d01841f
Add resistor-color-trio as side exercise
SleeplessByte aaca91a
Clean up solution
SleeplessByte 1460428
Fix error throwing test
SleeplessByte 32ac516
Unlock the resistor-color-trio by bob
SleeplessByte File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,26 @@ | ||
{ | ||
"root": true, | ||
"parser": "babel-eslint", | ||
"parserOptions": { | ||
"ecmaVersion": 7, | ||
"sourceType": "module" | ||
}, | ||
"env": { | ||
"es6": true, | ||
"node": true, | ||
"jest": true | ||
}, | ||
"extends": [ | ||
"eslint:recommended", | ||
"plugin:import/errors", | ||
"plugin:import/warnings" | ||
], | ||
"rules": { | ||
"linebreak-style": "off", | ||
|
||
"import/extensions": "off", | ||
"import/no-default-export": "off", | ||
"import/no-unresolved": "off", | ||
"import/prefer-default-export": "off" | ||
} | ||
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,73 @@ | ||
# Resistor Color Trio | ||
|
||
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: | ||
|
||
- 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. For example, if they printed a brown band (value 1) followed by a green band (value 5), it would translate to the number 15. | ||
- 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. | ||
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. | ||
The colors are mapped to the numbers from 0 to 9 in the sequence: | ||
|
||
Black - Brown - Red - Orange - Yellow - Green - Blue - Violet - Grey - White | ||
|
||
In `resistor-color duo` you decoded the first two colors. For instance: orange-orange got the main value `33`. | ||
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. | ||
For the exercise it doesn't matter what ohms really are. | ||
For example: | ||
|
||
- orange-orange-black would be 33 and no zeros, which becomes 33 ohms. | ||
- orange-orange-red would be 33 and 2 zeros, which becomes 3300 ohms. | ||
- orange-orange-orange would be 33 and 3 zeros, which becomes 33000 ohms. | ||
|
||
(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.) | ||
|
||
This exercise is about translating the colors into a label: | ||
|
||
> "... ohms" | ||
|
||
So an input of `"orange", "orange", "black"` should return: | ||
|
||
> "33 ohms" | ||
|
||
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. | ||
So an input of `"orange", "orange", "orange"` should return: | ||
|
||
> "33 kiloohms" | ||
|
||
## Setup | ||
|
||
Go through the setup instructions for Javascript to install the necessary | ||
dependencies: | ||
|
||
[https://exercism.io/tracks/javascript/installation](https://exercism.io/tracks/javascript/installation) | ||
|
||
## Requirements | ||
|
||
Install assignment dependencies: | ||
|
||
```bash | ||
$ npm install | ||
``` | ||
|
||
## Making the test suite pass | ||
|
||
Execute the tests with: | ||
|
||
```bash | ||
$ 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`. | ||
|
||
## Source | ||
|
||
Maud de Vries, Erik Schierboom [https://github.com/exercism/problem-specifications/issues/1549](https://github.com/exercism/problem-specifications/issues/1549) | ||
|
||
## Submitting Incomplete Solutions | ||
|
||
It's possible to submit an incomplete solution so you can see how others have | ||
completed the exercise. |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
module.exports = { | ||
presets: [ | ||
[ | ||
'@babel/env', | ||
{ | ||
targets: { | ||
node: 'current', | ||
}, | ||
useBuiltIns: false, | ||
}, | ||
|
||
], | ||
], | ||
}; |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,62 @@ | ||
const COLORS = [ | ||
'black', 'brown', 'red', 'orange', 'yellow', 'green', | ||
'blue', 'violet', 'grey', 'white', | ||
] | ||
|
||
const ONE_KILOOHM = 1000 | ||
|
||
class ArgumentError extends Error {} | ||
|
||
export class ResistorColorTrio { | ||
constructor([tens, ones, zeros]) { | ||
this.tens = tens | ||
this.ones = ones | ||
this.zeros = zeros | ||
} | ||
|
||
get value() { | ||
if (!this.isValid) { | ||
throw new ArgumentError('invalid color') | ||
} | ||
|
||
return this.significants() * this.multiplier() | ||
} | ||
|
||
get label() { | ||
return `Resistor value: ${this}` | ||
} | ||
|
||
get isValid() { | ||
return COLORS.indexOf(this.tens) > -1 | ||
&& COLORS.indexOf(this.ones) > -1 | ||
&& COLORS.indexOf(this.zeros) > -1 | ||
} | ||
|
||
toString() { | ||
const value = this.value | ||
return value < ONE_KILOOHM | ||
? `${value} ohms` | ||
: `${Math.floor(value / ONE_KILOOHM)} kiloohms` | ||
} | ||
|
||
/** | ||
* @private | ||
*/ | ||
significants() { | ||
return this.colorCode(this.tens) * 10 + this.colorCode(this.ones) | ||
} | ||
|
||
/** | ||
* @private | ||
*/ | ||
multiplier() { | ||
return Math.pow(10, this.colorCode(this.zeros)) | ||
} | ||
|
||
/** | ||
* @private | ||
*/ | ||
colorCode(color) { | ||
return COLORS.indexOf(color) | ||
} | ||
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,36 @@ | ||
{ | ||
"name": "exercism-javascript", | ||
"version": "1.1.0", | ||
"description": "Exercism exercises in Javascript.", | ||
"author": "Katrina Owen", | ||
"private": true, | ||
"repository": { | ||
"type": "git", | ||
"url": "https://github.com/exercism/javascript" | ||
}, | ||
"devDependencies": { | ||
"@babel/cli": "^7.5.5", | ||
"@babel/core": "^7.5.5", | ||
"@babel/preset-env": "^7.5.5", | ||
"@types/jest": "^24.0.16", | ||
"@types/node": "^12.6.8", | ||
"babel-eslint": "^10.0.2", | ||
"babel-jest": "^24.8.0", | ||
"eslint": "^6.1.0", | ||
"eslint-plugin-import": "^2.18.2", | ||
"jest": "^24.8.0" | ||
}, | ||
"jest": { | ||
"modulePathIgnorePatterns": [ | ||
"package.json" | ||
] | ||
}, | ||
"scripts": { | ||
"test": "jest --no-cache ./*", | ||
"watch": "jest --no-cache --watch ./*", | ||
"lint": "eslint .", | ||
"lint-test": "eslint . && jest --no-cache ./* " | ||
}, | ||
"license": "MIT", | ||
"dependencies": {} | ||
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
// | ||
// This is only a SKELETON file for the 'Resistor Color Duo' exercise. It's been provided as a | ||
// convenience to get you started writing code faster. | ||
// | ||
|
||
export class ResistorColorTrio { | ||
constructor() { | ||
throw new Error("Remove this statement and implement this function"); | ||
} | ||
|
||
label() { | ||
throw new Error("Remove this statement and implement this function"); | ||
} | ||
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,38 @@ | ||
import { ResistorColorTrio } from './resistor-color-trio.js'; | ||
|
||
function makeLabel({ value, unit }) { | ||
return `Resistor value: ${value} ${unit}` | ||
} | ||
|
||
describe('Resistor Color Trio', () => { | ||
test('Orange and orange and black', () => { | ||
expect(new ResistorColorTrio(["orange", "orange", "black"]).label) | ||
.toEqual(makeLabel({ value: 33, unit: "ohms" })); | ||
}); | ||
|
||
xtest('Blue and grey and brown', () => { | ||
expect(new ResistorColorTrio(["blue", "grey", "brown"]).label) | ||
.toEqual(makeLabel({ value: 680, unit: "ohms" })); | ||
}); | ||
|
||
xtest('Red and black and red', () => { | ||
expect(new ResistorColorTrio(["red", "black", "red"]).label) | ||
.toEqual(makeLabel({ value: 2, unit: "kiloohms" })); | ||
}); | ||
|
||
xtest('Green and brown and orange', () => { | ||
expect(new ResistorColorTrio(["green", "brown", "orange"]).label) | ||
.toEqual(makeLabel({ value: 51, unit: "kiloohms" })); | ||
}); | ||
|
||
xtest('Yellow and violet and yellow', () => { | ||
expect(new ResistorColorTrio(["yellow", "violet", "yellow"]).label) | ||
.toEqual(makeLabel({ value: 470, unit: "kiloohms" })); | ||
}); | ||
|
||
// optional: error | ||
xtest('Invalid color', () => { | ||
expect(() => new ResistorColorTrio(["yellow", "purple", "black"]).label) | ||
.toThrowError(/invalid color/); | ||
}); | ||
}); |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.