resistor-color-duo.ts 616 B

12345678910111213141516171819202122232425262728293031
  1. export function decodedValue(colors: string[]): number {
  2. if (colors.length < 2) {
  3. throw new Error("Unexpected number of colors");
  4. }
  5. // Ignore extra colors.
  6. colors = colors.slice(0, 2);
  7. const res = colorCode(colors[0])*10+colorCode(colors[1]);
  8. return res;
  9. }
  10. export const colorCode = (name: string): number => {
  11. const num = COLORS.indexOf(name.toLowerCase())
  12. if (num == -1) {
  13. throw new Error(`Invalid color name "${name}"`);
  14. }
  15. return num;
  16. }
  17. export const COLORS: string[] = [
  18. 'black',
  19. 'brown',
  20. 'red',
  21. 'orange',
  22. 'yellow',
  23. 'green',
  24. 'blue',
  25. 'violet',
  26. 'grey',
  27. 'white',
  28. ];