Resistor color trio
This commit is contained in:
13
typescript/resistor-color-trio/.eslintignore
Normal file
13
typescript/resistor-color-trio/.eslintignore
Normal file
@@ -0,0 +1,13 @@
|
||||
!.meta
|
||||
|
||||
# Protected or generated
|
||||
.git
|
||||
.vscode
|
||||
|
||||
# When using npm
|
||||
node_modules/*
|
||||
|
||||
# Configuration files
|
||||
.eslintrc.cjs
|
||||
babel.config.cjs
|
||||
jest.config.cjs
|
||||
38
typescript/resistor-color-trio/.eslintrc.cjs
Normal file
38
typescript/resistor-color-trio/.eslintrc.cjs
Normal file
@@ -0,0 +1,38 @@
|
||||
module.exports = {
|
||||
root: true,
|
||||
parserOptions: {
|
||||
tsconfigRootDir: __dirname,
|
||||
project: ['./tsconfig.json'],
|
||||
},
|
||||
overrides: [
|
||||
// Student provided files
|
||||
{
|
||||
files: ['*.ts'],
|
||||
excludedFiles: ['.meta/proof.ci.ts', '.meta/exemplar.ts', '*.test.ts'],
|
||||
extends: '@exercism/eslint-config-typescript',
|
||||
},
|
||||
// Exercism given tests
|
||||
{
|
||||
files: ['*.test.ts'],
|
||||
excludedFiles: ['custom.test.ts'],
|
||||
env: {
|
||||
jest: true,
|
||||
},
|
||||
extends: '@exercism/eslint-config-typescript/maintainers',
|
||||
},
|
||||
// Student provided tests
|
||||
{
|
||||
files: ['custom.test.ts'],
|
||||
env: {
|
||||
jest: true,
|
||||
},
|
||||
extends: '@exercism/eslint-config-typescript',
|
||||
},
|
||||
// Exercism provided files
|
||||
{
|
||||
files: ['.meta/proof.ci.ts', '.meta/exemplar.ts', '*.test.ts'],
|
||||
excludedFiles: ['custom.test.ts'],
|
||||
extends: '@exercism/eslint-config-typescript/maintainers',
|
||||
},
|
||||
],
|
||||
}
|
||||
12
typescript/resistor-color-trio/.exercism/config.json
Normal file
12
typescript/resistor-color-trio/.exercism/config.json
Normal file
@@ -0,0 +1,12 @@
|
||||
{
|
||||
"blurb": "Convert color codes, as used on resistors, to a human-readable label.",
|
||||
"authors": ["rodmagaldi"],
|
||||
"contributors": ["SleeplessByte"],
|
||||
"files": {
|
||||
"solution": ["resistor-color-trio.ts"],
|
||||
"test": ["resistor-color-trio.test.ts"],
|
||||
"example": [".meta/proof.ci.ts"]
|
||||
},
|
||||
"source": "Maud de Vries, Erik Schierboom",
|
||||
"source_url": "https://github.com/exercism/problem-specifications/issues/1549"
|
||||
}
|
||||
1
typescript/resistor-color-trio/.exercism/metadata.json
Normal file
1
typescript/resistor-color-trio/.exercism/metadata.json
Normal file
@@ -0,0 +1 @@
|
||||
{"track":"typescript","exercise":"resistor-color-trio","id":"11ea7adf045c4b05b9f496d5fcda16c9","url":"https://exercism.org/tracks/typescript/exercises/resistor-color-trio","handle":"briemens","is_requester":true,"auto_approve":false}
|
||||
44
typescript/resistor-color-trio/HELP.md
Normal file
44
typescript/resistor-color-trio/HELP.md
Normal file
@@ -0,0 +1,44 @@
|
||||
# Help
|
||||
|
||||
## Running the tests
|
||||
|
||||
Execute the tests with:
|
||||
|
||||
```bash
|
||||
$ yarn test
|
||||
```
|
||||
|
||||
## Skipped tests
|
||||
|
||||
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`.
|
||||
|
||||
## Submitting your solution
|
||||
|
||||
You can submit your solution using the `exercism submit resistor-color-trio.ts` command.
|
||||
This command will upload your solution to the Exercism website and print the solution page's URL.
|
||||
|
||||
It's possible to submit an incomplete solution which allows you to:
|
||||
|
||||
- See how others have completed the exercise
|
||||
- Request help from a mentor
|
||||
|
||||
## Need to get help?
|
||||
|
||||
If you'd like help solving the exercise, check the following pages:
|
||||
|
||||
- The [TypeScript track's documentation](https://exercism.org/docs/tracks/typescript)
|
||||
- [Exercism's support channel on gitter](https://gitter.im/exercism/support)
|
||||
- The [Frequently Asked Questions](https://exercism.org/docs/using/faqs)
|
||||
|
||||
Should those resources not suffice, you could submit your (incomplete) solution to request mentoring.
|
||||
|
||||
To get help if you're having trouble, you can use one of the following resources:
|
||||
|
||||
- [TypeScript QuickStart](https://www.typescriptlang.org/docs/handbook/release-notes/overview.html)
|
||||
- [ECMAScript 2015 Language Specification](https://www.ecma-international.org/wp-content/uploads/ECMA-262_6th_edition_june_2015.pdf) (pdf)
|
||||
- [Mozilla JavaScript Reference](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference)
|
||||
- [/r/typescript](https://www.reddit.com/r/typescript) is the TypeScript subreddit.
|
||||
- [StackOverflow](https://stackoverflow.com/questions/tagged/typescript) can be used to search for your problem and see if it has been answered already. You can also ask and answer questions.
|
||||
64
typescript/resistor-color-trio/README.md
Normal file
64
typescript/resistor-color-trio/README.md
Normal file
@@ -0,0 +1,64 @@
|
||||
# Resistor Color Trio
|
||||
|
||||
Welcome to Resistor Color Trio on Exercism's TypeScript Track.
|
||||
If you need help running the tests or submitting your code, check out `HELP.md`.
|
||||
|
||||
## Instructions
|
||||
|
||||
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 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 color bands are encoded as follows:
|
||||
|
||||
- Black: 0
|
||||
- Brown: 1
|
||||
- Red: 2
|
||||
- Orange: 3
|
||||
- Yellow: 4
|
||||
- Green: 5
|
||||
- Blue: 6
|
||||
- Violet: 7
|
||||
- Grey: 8
|
||||
- White: 9
|
||||
|
||||
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"
|
||||
|
||||
## Source
|
||||
|
||||
### Created by
|
||||
|
||||
- @rodmagaldi
|
||||
|
||||
### Contributed to by
|
||||
|
||||
- @SleeplessByte
|
||||
|
||||
### Based on
|
||||
|
||||
Maud de Vries, Erik Schierboom - https://github.com/exercism/problem-specifications/issues/1549
|
||||
4
typescript/resistor-color-trio/babel.config.cjs
Normal file
4
typescript/resistor-color-trio/babel.config.cjs
Normal file
@@ -0,0 +1,4 @@
|
||||
module.exports = {
|
||||
presets: ['@exercism/babel-preset-typescript'],
|
||||
plugins: [],
|
||||
}
|
||||
19
typescript/resistor-color-trio/jest.config.cjs
Normal file
19
typescript/resistor-color-trio/jest.config.cjs
Normal file
@@ -0,0 +1,19 @@
|
||||
module.exports = {
|
||||
verbose: true,
|
||||
projects: ['<rootDir>'],
|
||||
testMatch: [
|
||||
'**/__tests__/**/*.[jt]s?(x)',
|
||||
'**/test/**/*.[jt]s?(x)',
|
||||
'**/?(*.)+(spec|test).[jt]s?(x)',
|
||||
],
|
||||
testPathIgnorePatterns: [
|
||||
'/(?:production_)?node_modules/',
|
||||
'.d.ts$',
|
||||
'<rootDir>/test/fixtures',
|
||||
'<rootDir>/test/helpers',
|
||||
'__mocks__',
|
||||
],
|
||||
transform: {
|
||||
'^.+\\.[jt]sx?$': 'babel-jest',
|
||||
},
|
||||
}
|
||||
35
typescript/resistor-color-trio/package.json
Normal file
35
typescript/resistor-color-trio/package.json
Normal file
@@ -0,0 +1,35 @@
|
||||
{
|
||||
"name": "@exercism/typescript-resistor-color-trio",
|
||||
"version": "1.0.0",
|
||||
"description": "Exercism practice exercise on resistor-color-trio",
|
||||
"author": "Katrina Owen",
|
||||
"contributors": [
|
||||
"Derk-Jan Karrenbeld <derk-jan+git@karrenbeld.info> (https://derk-jan.com)"
|
||||
],
|
||||
"private": true,
|
||||
"repository": {
|
||||
"type": "git",
|
||||
"url": "https://github.com/exercism/typescript"
|
||||
},
|
||||
"type": "module",
|
||||
"engines": {
|
||||
"node": "^14.13.1 || >=16.0.0"
|
||||
},
|
||||
"devDependencies": {
|
||||
"@exercism/babel-preset-typescript": "^0.1.0",
|
||||
"@exercism/eslint-config-typescript": "^0.4.1",
|
||||
"@types/jest": "^27.4.0",
|
||||
"@types/node": "^16.11.24",
|
||||
"babel-jest": "^27.5.1",
|
||||
"core-js": "^3.21.0",
|
||||
"eslint": "^8.9.0",
|
||||
"jest": "^27.5.1",
|
||||
"typescript": "^4.5.4"
|
||||
},
|
||||
"scripts": {
|
||||
"test": "yarn lint:types && jest --no-cache",
|
||||
"lint": "yarn lint:types && yarn lint:ci",
|
||||
"lint:types": "yarn tsc --noEmit -p .",
|
||||
"lint:ci": "eslint . --ext .tsx,.ts"
|
||||
}
|
||||
}
|
||||
29
typescript/resistor-color-trio/resistor-color-trio.test.ts
Normal file
29
typescript/resistor-color-trio/resistor-color-trio.test.ts
Normal file
@@ -0,0 +1,29 @@
|
||||
import { decodedResistorValue } from "./resistor-color-trio";
|
||||
|
||||
describe("Resistor Colors", () => {
|
||||
it("Orange and orange and black", () => {
|
||||
expect(decodedResistorValue(["orange", "orange", "black"])).toEqual(
|
||||
"33 ohms"
|
||||
);
|
||||
});
|
||||
|
||||
it("Blue and grey and brown", () => {
|
||||
expect(decodedResistorValue(["blue", "grey", "brown"])).toEqual("680 ohms");
|
||||
});
|
||||
|
||||
it("Red and black and red", () => {
|
||||
expect(decodedResistorValue(["red", "black", "red"])).toEqual("2 kiloohms");
|
||||
});
|
||||
|
||||
it("Green and brown and orange", () => {
|
||||
expect(decodedResistorValue(["green", "brown", "orange"])).toEqual(
|
||||
"51 kiloohms"
|
||||
);
|
||||
});
|
||||
|
||||
it("Yellow and violet and yellow", () => {
|
||||
expect(decodedResistorValue(["yellow", "violet", "yellow"])).toEqual(
|
||||
"470 kiloohms"
|
||||
);
|
||||
});
|
||||
});
|
||||
31
typescript/resistor-color-trio/resistor-color-trio.ts
Normal file
31
typescript/resistor-color-trio/resistor-color-trio.ts
Normal file
@@ -0,0 +1,31 @@
|
||||
const colors: Record<string, number> = {
|
||||
black: 0,
|
||||
brown: 1,
|
||||
red: 2,
|
||||
orange: 3,
|
||||
yellow: 4,
|
||||
green: 5,
|
||||
blue: 6,
|
||||
violet: 7,
|
||||
grey: 8,
|
||||
white: 9,
|
||||
};
|
||||
|
||||
export const decodedResistorValue = ([color1, color2, color3]: [
|
||||
string,
|
||||
string,
|
||||
string
|
||||
]): string =>
|
||||
((
|
||||
value1: number = colors[color1],
|
||||
value2: number = colors[color2],
|
||||
isSecondAZero = value2 === 0,
|
||||
noOfZeros: number = colors[color3] + (isSecondAZero ? 1 : 0)
|
||||
): string =>
|
||||
((
|
||||
isKilo: boolean = noOfZeros >= 3,
|
||||
remainingNoOfZeros = isKilo ? noOfZeros - 3 : noOfZeros
|
||||
): string =>
|
||||
`${value1}${isSecondAZero ? "" : value2}${new Array(remainingNoOfZeros)
|
||||
.fill(0)
|
||||
.join("")} ${isKilo ? "kiloohms" : "ohms"}`)())();
|
||||
28
typescript/resistor-color-trio/tsconfig.json
Normal file
28
typescript/resistor-color-trio/tsconfig.json
Normal file
@@ -0,0 +1,28 @@
|
||||
{
|
||||
"display": "Configuration for Exercism TypeScript Exercises",
|
||||
"compilerOptions": {
|
||||
// Allows you to use the newest syntax, and have access to console.log
|
||||
// https://www.typescriptlang.org/tsconfig#lib
|
||||
"lib": ["ESNEXT", "dom"],
|
||||
// Make sure typescript is configured to output ESM
|
||||
// https://gist.github.com/sindresorhus/a39789f98801d908bbc7ff3ecc99d99c#how-can-i-make-my-typescript-project-output-esm
|
||||
"module": "ES2020",
|
||||
// Since this project is using babel, TypeScript may target something very
|
||||
// high, and babel will make sure it runs on your local Node version.
|
||||
// https://babeljs.io/docs/en/
|
||||
"target": "ESNext", // ESLint doesn't support this yet: "es2022",
|
||||
|
||||
"strict": true,
|
||||
"esModuleInterop": true,
|
||||
"skipLibCheck": true,
|
||||
"forceConsistentCasingInFileNames": true,
|
||||
|
||||
// Because we'll be using babel: ensure that Babel can safely transpile
|
||||
// files in the TypeScript project.
|
||||
//
|
||||
// https://babeljs.io/docs/en/babel-plugin-transform-typescript/#caveats
|
||||
"isolatedModules": true
|
||||
},
|
||||
"include": ["*.ts", "*.tsx", ".meta/*.ts", ".meta/*.tsx"],
|
||||
"exclude": ["node_modules"]
|
||||
}
|
||||
4553
typescript/resistor-color-trio/yarn.lock
Normal file
4553
typescript/resistor-color-trio/yarn.lock
Normal file
File diff suppressed because it is too large
Load Diff
Reference in New Issue
Block a user