#Resistor-color.js troubles

25 messages · Page 1 of 1 (latest)

blazing nexus
#

So I have been trying to solve this for awhile now and can not wrap my head around why test 4 keeps returning the whole object instead of just the keys.
This is what I have so far:
`export const colorCode = (color) => {
const colorsKeys = Object.keys(COLORS);
if (color === COLORS) {
return colorsKeys;
} else {
return COLORS[color];
}
return colorsKeys;
}

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

I have tried to re word this in many different ways, different conditionals, == versus ===, and still keep running into the same problem on test 4. Whenever I try and return colorsKeys to satisfy the last test it returns the entire object, values and all. I have even tried just returning just colorsKeys and removing all other code besides the line where I assign colorsKeys its value. In the first 3 tests colorsKeys functions as expected by returning just the keys from the COLORS object, but when test 4 is run it returns the entire object with the value pair.

I can not figure out why colorsKeys would return two different values. Any help would be greatly appreciated!

shut spire
#

What does the test output say?

blazing nexus
#

`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}`

shut spire
#

I think this exercise asks you to export a COLORS which is a list of the colors

blazing nexus
#

Can you elaborate? colorCode and COLORS are both exported.

elfin river
#

The tests expect COLORS to be an array of strings. You can't use an object for that constant.

#
  test('Colors', () => {
    expect(COLORS).toEqual(["black","brown","red","orange","yellow","green","blue","violet","grey","white"])
  })
#

You'll find the Array method indexOf to be helpful.

blazing nexus
#

When I console log colorsKeys in tests 1-3 it returns an array, like the test expects. This was really throwing me off.

#

On test 4 it logs the whole object instead of just the value I assigned to colorsKeys.

elfin river
#

For the colorCode function, for which test is the color argument equal to the COLORS constant? Do you need that condition?

blazing nexus
#

They pass COLORS as the argument in test 4, so I was checking to see if color was equal to COLORS

elfin river
#

For test 4, what function does the test call?

blazing nexus
#

expect(COLORS).toEqual([ 'black', 'brown', 'red', 'orange', 'yellow', 'green', 'blue', 'violet', 'grey', 'white', ]);

elfin river
#

Does it call colorCode?

blazing nexus
#

Oh it doesn't... Just realized that

elfin river
#

In the spec file, pay close attention to what gets imported

blazing nexus
#

Thats why it was logging two different things

elfin river
#

This is how Test-Driven Development (TDD) works at exercism: the provided test suite precisely defines the requirements for your code.

blazing nexus
#

Wait so was I not supposed to define the COLORS array with the value pairs?

elfin river
#

That's right.

blazing nexus
#

My brain hurts a bit less now, thank you!

cold badge
#

Hey guys, I'm having trouble with this one too, I ran my code on vscode and it worked, but none of the tests passed. Here's what i did:

`export const colorCode = (color:string) => {
const COLORS_ENCODED: {[key:string]:number} = {
"Black": 0,
"Brown": 1,
"Red": 2,
"Orange": 3,
"Yellow": 4,
"Green": 5,
"Blue": 6,
"Violet": 7,
"Grey": 8,
"White": 9,
}
return COLORS_ENCODED[color];

}

export const COLORS: string[] = ["Black", "Brown", "Red", "Orange", "Yellow", "Green", "Blue", "Violet", "Grey", "White"];
Could anyone tell you what the problem is? also, looking at the tests some wore written like xit('returns all colors', () =>... should not be likeit('returns all colors', () =>...`?

shut spire
#

Please open a new thread for new support requests, @cold badge

elfin river