Choosing the right metric prefix

Hello,

I am hoping for some advice on formatting the answer for this exercise. Initially, I built the ohm number the same way as resistor-color-duo and put the third color band into an array to get the corresponding prefix. This gave the correct number of ohms but failed certain tests because it was not in the expected units. For example:

From there I thought about changing it to actually make the full number in ohms and then divide by ten until the number was of a certain length, but that runs into an issue where many numbers end at 3 digits (i.e. the 470 kiloohms) but others reduce all the way (2 kiloohms is expected in another test, rather than 20 hectoohms).

I’m not certain how to go about reducing the numbers properly in a way that isn’t just accounting for specific tests. Any advice is appreciated.

I would recommend computing the number first. Then you can figure out the units. The units are k, m, g – factors of 1,000 1,000,000 and 1,000,000,000. So you should be dividing by 1,000 and not by 10.

1 Like

Thanks! Not sure why I was stuck on tens. It was super simple when I adjusted to thousands.