Hello All^^ I apologize for the simplicity of this question (and my JS comprehension), beforehand. Could someone explain why the following code returns the key value in the object array? I would expect that array-name[n] would return the key, rather than the value in the array. I thought that array-name[n][1] would produce the value (name of the shape in this example) in this function ( i.e., if 4 is entered, "square" would be returned). For the background, this was an edabit challenge that I solved. However, that was not entirely on purpose(I was initially trying to use a loop to perform the same function for about a half hour, to no avail). To digress, I would simply like to understand this more thoroughly, if possible. I appreciate your time.
const polygons = {
1 : "circle" ,
2 : "semi-circle",
3 : "triangle",
4 : "square",
5 : "pentagon",
6 : "hexagon",
7 : "heptagon",
8 : "octagon",
9 : "nonagon",
10 : "decagon"
};
return polygons[n];
}
polygonsisn't an array here) work. If you want the keys uselet keys = Object.keys(polygons);