Skip to content

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
merged 5 commits into from
Oct 1, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 12 additions & 1 deletion config.json
Original file line number Diff line number Diff line change
Expand Up @@ -800,6 +800,17 @@
"strings"
]
},
{
"slug": "resistor-color-trio",
"uuid": "7170d6a3-a32f-44d7-b82e-524485c58aaf",
"core": false,
"unlocked_by": "bob",
"difficulty": 5,
"topics": [
"conditionals",
"loops"
]
},
{
"slug": "say",
"uuid": "12989bb3-c593-4f68-bea4-e2c5b76bc3c0",
Expand Down Expand Up @@ -1408,4 +1419,4 @@
"deprecated": true
}
]
}
}
26 changes: 26 additions & 0 deletions exercises/resistor-color-trio/.eslintrc
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"
}
}
73 changes: 73 additions & 0 deletions exercises/resistor-color-trio/README.md
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.
14 changes: 14 additions & 0 deletions exercises/resistor-color-trio/babel.config.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
module.exports = {
presets: [
[
'@babel/env',
{
targets: {
node: 'current',
},
useBuiltIns: false,
},

],
],
};
62 changes: 62 additions & 0 deletions exercises/resistor-color-trio/example.js
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)
}
}
36 changes: 36 additions & 0 deletions exercises/resistor-color-trio/package.json
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": {}
}
14 changes: 14 additions & 0 deletions exercises/resistor-color-trio/resistor-color-trio.js
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");
}
}
38 changes: 38 additions & 0 deletions exercises/resistor-color-trio/resistor-color-trio.spec.js
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/);
});
});