Skip to content

Add analyzer for resistor-color-duo #14

Closed
@SleeplessByte

Description

@SleeplessByte

This issue is for discussion and assignment for the resistor-color-duo core exercise in the javascript track.

🔗 implementation | mentor-notes | problem-specification


This exercise introduces map and join to the student, as well as converting from and to number, and builds upon their knowledge of arrays from the previous exercise resistor-color.

Optimal solution(s)

export const COLORS = [
  'black', 'brown', 'red', 'orange', 'yellow', 'green', 'blue', 'violet',
  'grey', 'white',
];

function colorCode(color) {
  return COLORS.indexOf(color)
}

export function value(colors) {
  return Number(colors.map(colorCode).join(''))
}

A student who uses a reducer on the reverse of the input. This is a technique that comes from other languages as well as algorithm solving in university and is perfectly valid. It's one of the ways to do this without conversion to a String. Such a solution looks like this:

export const COLORS = [
  'black', 'brown', 'red', 'orange', 'yellow', 'green', 'blue', 'violet',
  'grey', 'white',
];

export function value(colors) {
  return colors
    .reverse()
    .reduce(
      (value, color, i) => {
        // Find the index, then shift it in base 10, i places to the left
        return COLORS.indexOf(color) * (10 ** i) + value
      }
      , 0
    );
}

SHOULD comment and disapprove

Cases we SHOULD comment on with this analyzer:

  • Comment to use the resistor-color solution instead of manually doing indexOf
  • Comment to use the resistor-color solution instead of a set of conditionals or a switch
  • Comment to use Number instead of parseXXX
  • Comment to use Number instead of unary +
  • Comment to use join instead of reduce (when using reduce to build the string)
  • Comment to use join (converts to string) instead of explicit toString
  • Comment to use an Array for COLORS instead of Object

COULD comment (tips)

Cases we COULD comment on with this analyzer:

  • Doing an export with a specifier (at the bottom of the file) can be done export const inline

Metadata

Metadata

Assignees

Labels

No labels
No labels

Type

No type

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions