From ce3306d2b692813c2660f90d569dcaa20fc777d6 Mon Sep 17 00:00:00 2001 From: "exercism-solutions-syncer[bot]" <211797793+exercism-solutions-syncer[bot]@users.noreply.github.com> Date: Mon, 18 May 2026 22:13:55 +0000 Subject: [PATCH] [Sync Iteration] typescript/resistor-color-trio/1 --- .../1/resistor-color-trio.ts | 54 +++++++++++++++++++ 1 file changed, 54 insertions(+) create mode 100644 solutions/typescript/resistor-color-trio/1/resistor-color-trio.ts diff --git a/solutions/typescript/resistor-color-trio/1/resistor-color-trio.ts b/solutions/typescript/resistor-color-trio/1/resistor-color-trio.ts new file mode 100644 index 0000000..4a42495 --- /dev/null +++ b/solutions/typescript/resistor-color-trio/1/resistor-color-trio.ts @@ -0,0 +1,54 @@ +type Color = + | "black" + | "brown" + | "red" + | "orange" + | "yellow" + | "green" + | "blue" + | "violet" + | "grey" + | "white"; + +const COLORS = [ + "black", + "brown", + "red", + "orange", + "yellow", + "green", + "blue", + "violet", + "grey", + "white", +]; + +type Magnitude = "giga" | "mega" | "kilo" | ""; + +export function decodedResistorValue(colors: Color[]) { + const [code1, code2, zeros, ...arg] = colors; + const block = COLORS.indexOf(code1) * 10 + COLORS.indexOf(code2); + const total = block * Math.pow(10, COLORS.indexOf(zeros)); + let number: number; + let magnitude: Magnitude; + const giga = 1_000_000_000; + const mega = 1_000_000; + const kilo = 1_000; + const isGiga = total / giga > 0 && Number.isInteger(total / giga); + const isMega = total / mega > 0 && Number.isInteger(total / mega); + const isKilo = total / kilo > 0 && Number.isInteger(total / kilo); + if (isGiga) { + number = total / giga; + magnitude = "giga"; + } else if (isMega) { + number = total / mega; + magnitude = "mega"; + } else if (isKilo) { + number = total / kilo; + magnitude = "kilo"; + } else { + number = total; + magnitude = ""; + } + return `${number} ${magnitude}ohms`; +}