| 4 | 4 | // |
| 5 | 5 | |
| 6 | 6 | export class ResistorColorTrio { |
| 7 | | constructor() { |
| 8 | | throw new Error('Remove this line and implement the function'); |
| 7 | constructor(colors) { |
| 8 | // Edge Case: Missing or empty colors array |
| 9 | if (!colors || colors.length === 0) { |
| 10 | throw new Error('missing colors'); |
| 11 | } |
| 12 | |
| 13 | // Edge Case: Less than 3 colors provided |
| 14 | if (colors.length < 3) { |
| 15 | throw new Error('missing colors'); |
| 16 | } |
| 17 | |
| 18 | this.colors = colors; |
| 19 | this.colorValues = { |
| 20 | 'black': 0, |
| 21 | 'brown': 1, |
| 22 | 'red': 2, |
| 23 | 'orange': 3, |
| 24 | 'yellow': 4, |
| 25 | 'green': 5, |
| 26 | 'blue': 6, |
| 27 | 'violet': 7, |
| 28 | 'grey': 8, |
| 29 | 'white': 9 |
| 30 | }; |
| 9 | 31 | } |
| 10 | 32 | |
| 11 | | label() { |
| 12 | | throw new Error('Remove this line and implement the function'); |
| 33 | get label() { |
| 34 | const [first, second, third] = this.colors; |
| 35 | |
| 36 | // Edge Case: Invalid color names |
| 37 | if (!(first in this.colorValues) || !(second in this.colorValues) || !(third in this.colorValues)) { |
| 38 | throw new Error('invalid color'); |
| 39 | } |
| 40 | |
| 41 | const firstValue = this.colorValues[first]; |
| 42 | const secondValue = this.colorValues[second]; |
| 43 | const zeros = this.colorValues[third]; |
| 44 | |
| 45 | // Calculate the base value |
| 46 | const baseValue = firstValue * 10 + secondValue; |
| 47 | |
| 48 | // Calculate the total ohms value |
| 49 | let ohms = baseValue * Math.pow(10, zeros); |
| 50 | |
| 51 | // Determine the appropriate unit based on the magnitude |
| 52 | let unit = 'ohms'; |
| 53 | if (ohms >= 1000000000) { |
| 54 | ohms = ohms / 1000000000; |
| 55 | unit = 'gigaohms'; |
| 56 | } else if (ohms >= 1000000) { |
| 57 | ohms = ohms / 1000000; |
| 58 | unit = 'megaohms'; |
| 59 | } else if (ohms >= 1000) { |
| 60 | ohms = ohms / 1000; |
| 61 | unit = 'kiloohms'; |
| 62 | } |
| 63 | |
| 64 | return `${ohms} ${unit}`; |
| 13 | 65 | } |
| 14 | 66 | } |
| 67 | |
| 68 | // Handled Edge Cases: missing colors array, less than 3 colors provided, invalid color names |