[Resistor Color Trio] Proposal to add mentor notes

I’ve started mentoring three sessions of the Resistor Color Trio exercise in the last week, which makes me think that it is a frequent source of confusion.

I think this is an excellent opportunity to talk about the power of using as const objects and keyof typeof, which are features of TS that I use almost daily in my job as a full stack dev. It allows you to write the constant like so:

const COLOR_TO_NUMBER = {
  "black": 0,
  "brown": 1,
  "red": 2,
  "orange": 3,
  "yellow": 4,
  "green": 5,
  "blue": 6,
  "violet": 7,
  "grey": 8,
  "white": 9
} as const;

and then the type of the input like so:

type color = keyof typeof COLOR_TO_NUMBER;

which leads to a super succinct solution of the main function (with the final transformation of the unit extracted to a helper function)

export const decodedResistorValue = ([colorOne, colorTwo, colorThree]: color[]) => {
  const multiplier = 10 * COLOR_TO_NUMBER[colorOne] + COLOR_TO_NUMBER[colorTwo];
  return addUnit(multiplier * Math.pow(10, COLOR_TO_NUMBER[colorThree]))
}

const addUnit = (resistanceValue: number): string => {
  if (resistanceValue >= 10e8) return `${Math.floor(resistanceValue / 10e8)} gigaohms`
  if (resistanceValue >= 10e5) return `${Math.floor(resistanceValue / 10e5)} megaohms`
  if (resistanceValue >= 10e2) return `${Math.floor(resistanceValue / 10e2)} kiloohms`
  return `${resistanceValue} ohms`
}

I think adding mentor notes that explains this would benefit the community (I’ve seen some very strange ways of approaching this exercise), but I would like to discuss it here to make sure we arrive at something that will add the most value. So, I welcome everyone to shoot suggestions. :raised_hands:

Hello! Thanks for mentoring and this suggestion.

Mentor notes only show to mentors. What you might really want is an Approach or an Article for this, which shows to students.

I’d be very open for that.

See:

My instinct is that what you’re proposing is an Approach, as it covers one specific way to solve the exercise.

Thanks! :slight_smile:

The argument constraints are too weak; it allows an arbitrary number of color strings. You should consider a tuple type.

Also, you can select the unit from an array if you separate mantissa and exponent:

const COLORS = {
  black: 0,
  brown: 1,
  red: 2,
  orange: 3,
  yellow: 4,
  green: 5,
  blue: 6,
  violet: 7,
  grey: 8,
  white: 9
} as const;
const multipliers = ["", "kilo", "mega", "giga"] as const;
type Color = keyof typeof COLORS;
export function decodedResistorValue([c1, c2, c3]: [Color, Color, Color]) {
  const mantissa = (COLORS[c1] + COLORS[c2] * 0.1) * (c2 === "black" ? 1 : 10),
    exponent = COLORS[c3] + (c2 === "black" ? 1 : 0);
  return `${mantissa * 10 ** (exponent % 3)} ${multipliers[Math.floor(exponent / 3)]}ohms`;
}

That being said, the challenges in this functions is the argument constraints and the unit modifier. The strategies to convert the number are either mathematical or string concatenation-based; the modifier can be selected by length or mathematical.

Hi Jeremy!

Thanks for the response (and for building this awesome platform :rocket:)

I did not know about the approaches and articles, that seems like a great option for my solution specifically, so I will definitely try to write one.

However, I would still like to collaborate on writing more generic mentor notes too (which does not have to include my solution). I just feel like the amount of people asking for mentoring on this exercise is an indicator that mentor docs would be useful. I was hoping to use this thread to gather the various topics and notes that we can include in the mentor notes. I’m completely open for the final result to take any structure that would contribute towards this goal. :smile:

1 Like

Thanks for these suggestions! The tuple is definitely a good shout. The approach of doing the multipliers in an array is also interesting, but I’m inclined to think that it decreases readability for not that much gain. But it certainly is cool a cool way to solve the problem.

I think having different sections for the function signature, the types used for the conversion, the conversion itself, and for adding the unit sounds like a good starting point. What do you think?

1 Like

You can certainly write mentor docs - that’s great too. It’s just they don’t necessarily directly help students - they more help mentors mentor well (which might also of course benefit students). Whereas the Approaches/Articles are student-facing so of higher immediate impact.

I’d suggest starting by writing an Approach, focussing on your preferred way of doing it, as you can be quite autonomous in that. Then the mentor notes can maybe be more of a collaberative effort :slight_smile:

2 Likes