Skip to content

Commit d672bfd

Browse files
committed
feat: complete resistor-color-trio
1 parent e44dbe9 commit d672bfd

File tree

2 files changed

+44
-12
lines changed

2 files changed

+44
-12
lines changed

typescript/resistor-color-trio/resistor-color-trio.test.ts

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import { describe, it, expect, xit } from '@jest/globals'
1+
import { describe, it, expect } from '@jest/globals'
22
import { decodedResistorValue } from './resistor-color-trio.ts'
33

44
describe('Resistor Colors', () => {
@@ -8,47 +8,47 @@ describe('Resistor Colors', () => {
88
)
99
})
1010

11-
xit('Blue and grey and brown', () => {
11+
it('Blue and grey and brown', () => {
1212
expect(decodedResistorValue(['blue', 'grey', 'brown'])).toEqual('680 ohms')
1313
})
1414

15-
xit('Red and black and red', () => {
15+
it('Red and black and red', () => {
1616
expect(decodedResistorValue(['red', 'black', 'red'])).toEqual('2 kiloohms')
1717
})
1818

19-
xit('Green and brown and orange', () => {
19+
it('Green and brown and orange', () => {
2020
expect(decodedResistorValue(['green', 'brown', 'orange'])).toEqual(
2121
'51 kiloohms'
2222
)
2323
})
2424

25-
xit('Yellow and violet and yellow', () => {
25+
it('Yellow and violet and yellow', () => {
2626
expect(decodedResistorValue(['yellow', 'violet', 'yellow'])).toEqual(
2727
'470 kiloohms'
2828
)
2929
})
3030

31-
xit('Blue and violet and blue', () => {
31+
it('Blue and violet and blue', () => {
3232
expect(decodedResistorValue(['blue', 'violet', 'blue'])).toEqual(
3333
'67 megaohms'
3434
)
3535
})
3636

37-
xit('Minimum possible value', () => {
37+
it('Minimum possible value', () => {
3838
expect(decodedResistorValue(['black', 'black', 'black'])).toEqual('0 ohms')
3939
})
4040

41-
xit('Maximum possible value', () => {
41+
it('Maximum possible value', () => {
4242
expect(decodedResistorValue(['white', 'white', 'white'])).toEqual(
4343
'99 gigaohms'
4444
)
4545
})
4646

47-
xit('First two colors make an invalid octal number', () => {
47+
it('First two colors make an invalid octal number', () => {
4848
expect(decodedResistorValue(['black', 'grey', 'black'])).toEqual('8 ohms')
4949
})
5050

51-
xit('Ignore extra colors', () => {
51+
it('Ignore extra colors', () => {
5252
expect(decodedResistorValue(['blue', 'green', 'yellow', 'orange'])).toEqual(
5353
'650 kiloohms'
5454
)
Lines changed: 34 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,35 @@
1-
export function decodedResistorValue() {
2-
throw new Error('Remove this statement and implement this function')
1+
const COLORS = [
2+
'black',
3+
'brown',
4+
'red',
5+
'orange',
6+
'yellow',
7+
'green',
8+
'blue',
9+
'violet',
10+
'grey',
11+
'white',
12+
] as const;
13+
14+
const METRIC_PREFIXES = ['ohms', 'kiloohms', 'megaohms', 'gigaohms'] as const;
15+
16+
type Color = (typeof COLORS)[number];
17+
18+
const colorCode = (color: Color): number => COLORS.indexOf(color);
19+
20+
export function decodedResistorValue(colors: readonly string[]): string {
21+
const validColors = colors.slice(0, 3).filter((color): color is Color => COLORS.includes(color as Color));
22+
const [tens, ones, zeros] = validColors.map(colorCode);
23+
24+
const baseValue = tens * 10 + ones;
25+
const magnitude = baseValue * Math.pow(10, zeros);
26+
27+
if (magnitude === 0) {
28+
return '0 ohms';
29+
}
30+
31+
const exponent = Math.floor(Math.log10(magnitude) / 3);
32+
const adjustedValue = magnitude / Math.pow(10, exponent * 3);
33+
34+
return `${adjustedValue} ${METRIC_PREFIXES[exponent]}`;
335
}

0 commit comments

Comments
 (0)