| 8 | 8 | * @param {string[]} colors |
| 9 | 9 | */ |
| 10 | 10 | constructor(colors) { |
| 11 | | throw new Error('Remove this line and implement the function'); |
| 11 | if (colors.length < 3) { |
| 12 | throw new Error('At least three colors need to be present'); |
| 13 | } |
| 14 | |
| 15 | this.colors = colors; |
| 16 | this.colorValues = { |
| 17 | 'black': 0, |
| 18 | 'brown': 1, |
| 19 | 'red': 2, |
| 20 | 'orange': 3, |
| 21 | 'yellow': 4, |
| 22 | 'green': 5, |
| 23 | 'blue': 6, |
| 24 | 'violet': 7, |
| 25 | 'grey': 8, |
| 26 | 'white': 9 |
| 27 | }; |
| 12 | 28 | } |
| 13 | 29 | |
| 14 | 30 | /** |
| 15 | 31 | * @returns {string} |
| 16 | 32 | */ |
| 17 | 33 | label() { |
| 18 | | throw new Error('Remove this line and implement the function'); |
| 34 | // Edge Case: Invalid color inputs not in the defined color list |
| 35 | if (!this.colorValues.hasOwnProperty(this.colors[0]) || |
| 36 | !this.colorValues.hasOwnProperty(this.colors[1]) || |
| 37 | !this.colorValues.hasOwnProperty(this.colors[2])) { |
| 38 | throw new Error('Invalid color'); |
| 39 | } |
| 40 | |
| 41 | const firstValue = this.colorValues[this.colors[0]]; |
| 42 | const secondValue = this.colorValues[this.colors[1]]; |
| 43 | const zeros = this.colorValues[this.colors[2]]; |
| 44 | |
| 45 | // Edge Case: Handle calculation of the resistance value |
| 46 | let resistance = (firstValue * 10 + secondValue) * Math.pow(10, zeros); |
| 47 | |
| 48 | // Edge Case: Convert to appropriate metric prefix based on magnitude |
| 49 | if (resistance >= 1000000000) { |
| 50 | return `${resistance / 1000000000} gigaohms`; |
| 51 | } else if (resistance >= 1000000) { |
| 52 | return `${resistance / 1000000} megaohms`; |
| 53 | } else if (resistance >= 1000) { |
| 54 | return `${resistance / 1000} kiloohms`; |
| 55 | } else { |
| 56 | return `${resistance} ohms`; |
| 57 | } |
| 19 | 58 | } |
| 20 | 59 | } |
| 60 | |
| 61 | // Handled Edge Cases: |
| 62 | // 1. Invalid color inputs not in the defined color list |
| 63 | // 2. Empty or missing color inputs (through constructor check) |
| 64 | // 3. Conversion to appropriate metric prefixes (kilo, mega, giga) |
| 65 | // 4. Proper calculation of resistance values based on color codes |