Traffic Lights
The aim of this puzzle: Turn the bottom traffic light green.
Walkthrough of the solution: When you run the starter code, you see that the bottom circle is purple instead of green. If you check the code, it seems like the problem is in the last command: bottomCircle.attr('fill', 'purple');
So, what is actually going on? bottomCircle
is an identifier, or variable, that represents the bottom circle as you would expect. .attr()
is used to choose an attribute or property of the whichever identifier itās attached to and change the value of that attribute. In this line, the .attr()
is finding the 'fill'
attribute which is the color that the shape is filled in with. You can see itās being filled with 'purple'
when it should be 'green'
. All you have to do is change the value of that one string.
Sample code solution:
(Tap below to reveal)
topCircle.attr('fill', 'red');
middleCircle.attr('fill', 'orange');
bottomCircle.attr('fill', 'green');
JavaScript Concepts: Identifiers, Member Expression
D3 Concepts: .attr(āfillā,)
Additional Code (hidden code that runs before the puzzleās code):
var topCircle = svg.append('circle').attr('r', 25).attr('cx', 25).attr('cy', 25);
var middleCircle = svg.append('circle').attr('r', 25).attr('cx', 25).attr('cy',75);
var bottomCircle = svg.append('circle').attr('r', 25).attr('cx', 25).attr('cy',125);