Hi! I had that problem when I tried to solve the Resist Color exercise on the JavaScript track last test wasn’t want to pass.
I have an object with colour and number pairs ( black: 0, brown: 1, …etc.). Tests when the program expects values that pass ( return COLORS[color] ), but the last one wants an array of colours. I thought to use Object.keys() but it not works. I tried it in the console and worked how it was expected.
Any idea?
In general, in case of unexpected test failures I recommend reading the test’s source code. If you are working locally, then you already have the relevant file. If you are using the web editor, then you need to look it up in the track repo: GitHub - exercism/javascript: Exercism exercises in JavaScript.. It is named <exercise>.spec.js
.
Also in general, when asking for help with your own code: show your code, and show the unexpected result. (Via code block please, not via screenshot.)
Code block:
```python
print("Hello, World!")
```
print("Hello, World!")
export const colorCode = (color) => {
return !color ? Object.keys(COLORS) : COLORS[color];
};
export const COLORS = {black: 0, brown: 1, red: 2, orange: 3, yellow: 4,
green: 5, blue: 6, violet: 7, grey: 8, white: 9,};
Error: expect(received).toEqual(expected) // deep equality
Expected: ["black", "brown", "red", "orange", "yellow", "green", "blue", "violet", "grey", "white"]
Received: {"black": 0, "blue": 6, "brown": 1, "green": 5, "grey": 8, "orange": 3, "red": 2, "violet": 7, "white": 9, "yellow": 4}
xtest('Colors', () => {
expect(COLORS).toEqual([
'black',
'brown',
'red',
'orange',
'yellow',
'green',
'blue',
'violet',
'grey',
'white',
]);
I reckon I find the problem: the test is supposed to be expect(colorCode()) instead of expect(COLORS).
How to report it?
Thanks for contributing to Exercism!
I’m not sure why you think this is a problem. The test expects a variable (constant is better practice) called COLORS
, that’s all there is to it.
Is there an instruction to return the list of color names if null
is passed in to the function?
I do think, however, that this exercise’s instructions could explain that this is required - or even better, remove the tests. There are multiple idiomatic ways to do this, and having an array (and using .indexOf
) is not necessarily the best one.
It’s necessarily the best one.
Thank everyone, I solved it. Although I think the last test is still bad.
export const colorCode = (color) => {
return !color ? COLORS : NUMBERS[COLORS.indexOf(color)];
};
export const COLORS =['black','brown','red','orange','yellow',
'green','blue','violet','grey','white']
export const NUMBERS = [0, 1, 2, 3, 4, 5, 6, 7, 8, 9];
You can just say return !color ? COLORS : COLORS.indexOf(color);
I just realized the pun here… good one .
@Firebird19 this exercise was created before we had the syllabus. It’s purpose is to have you use an array and therefor indexOf.
There were students having issues with the data structures and thus the test was added to return the colors as an array.
The tests and exercise still ALLOW you to have an object with colours to value mapping as long as the exported array is a list of colours, however, the easiest solution (and also the most maintainable) is a single array of the colours and a function that calls indexOf.