Closed
Description
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 doingindexOf
- Comment to use the
resistor-color
solution instead of a set of conditionals or a switch - Comment to use
Number
instead ofparseXXX
- Comment to use
Number
instead of unary+
- Comment to use
join
instead ofreduce
(when usingreduce
to build the string) - Comment to use
join
(converts to string) instead of explicittoString
- Comment to use an
Array
forCOLORS
instead ofObject
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 doneexport const
inline
Metadata
Metadata
Assignees
Labels
No labels