curriculum/challenges/english/blocks/data-visualization-with-d3/587d7fa7367417b2b2512bc7.md
D3 is about visualization and presentation of data. It's likely you'll want to change the styling of elements based on the data.
For example, you may want to color a data point blue if it has a value less than 20, and red otherwise. You can use a callback function in the style() method and include the conditional logic. The callback function uses the d parameter to represent the data point:
selection.style('color', d => {});
The style() method is not limited to setting the color - it can be used with other CSS properties as well.
Add the style() method to the code in the editor to set the color of the h2 elements conditionally. Write the callback function so if the data value is less than 20, it returns red, otherwise it returns green.
Note: You can use if-else logic, or the ternary operator.
The first h2 should have a color of red.
const headingTwoFirst = document.querySelector('h2');
assert.exists(headingTwoFirst);
const color = window.getComputedStyle(headingTwoFirst)['color'];
assert.strictEqual(color, 'rgb(255, 0, 0)');
The second h2 should have a color of green.
const headingTwoSecond = document.querySelectorAll('h2')[1];
assert.exists(headingTwoSecond);
const color = window.getComputedStyle(headingTwoSecond)['color'];
assert.strictEqual(color, 'rgb(0, 128, 0)');
The third h2 should have a color of green.
const headingTwoThird = document.querySelectorAll('h2')[2];
assert.exists(headingTwoThird);
const color = window.getComputedStyle(headingTwoThird)['color'];
assert.strictEqual(color, 'rgb(0, 128, 0)');
The fourth h2 should have a color of red.
const headingTwoFourth = document.querySelectorAll('h2')[3];
assert.exists(headingTwoFourth);
const color = window.getComputedStyle(headingTwoFourth)['color'];
assert.strictEqual(color, 'rgb(255, 0, 0)');
The fifth h2 should have a color of green.
const headingTwoFifth = document.querySelectorAll('h2')[4];
assert.exists(headingTwoFifth);
const color = window.getComputedStyle(headingTwoFifth)['color'];
assert.strictEqual(color, 'rgb(0, 128, 0)');
The sixth h2 should have a color of red.
const headingTwoSixth = document.querySelectorAll('h2')[5];
assert.exists(headingTwoSixth);
const color = window.getComputedStyle(headingTwoSixth)['color'];
assert.strictEqual(color, 'rgb(255, 0, 0)');
The seventh h2 should have a color of green.
const headingTwoSeventh = document.querySelectorAll('h2')[6];
assert.exists(headingTwoSeventh);
const color = window.getComputedStyle(headingTwoSeventh)['color'];
assert.strictEqual(color, 'rgb(0, 128, 0)');
The eighth h2 should have a color of red.
const headingTwoEighth = document.querySelectorAll('h2')[7];
assert.exists(headingTwoEighth);
const color = window.getComputedStyle(headingTwoEighth)['color'];
assert.strictEqual(color, 'rgb(255, 0, 0)');
The ninth h2 should have a color of red.
const headingTwoNinth = document.querySelectorAll('h2')[8];
assert.exists(headingTwoNinth);
const color = window.getComputedStyle(headingTwoNinth)['color'];
assert.strictEqual(color, 'rgb(255, 0, 0)');
<body>
<script>
const dataset = [12, 31, 22, 17, 25, 18, 29, 14, 9];
d3.select('body')
.selectAll('h2')
.data(dataset)
.enter()
.append('h2')
.text(d => d + ' USD');
// Add your code below this line
// Add your code above this line
</script>
</body>
<body>
<script>
const dataset = [12, 31, 22, 17, 25, 18, 29, 14, 9];
d3.select('body')
.selectAll('h2')
.data(dataset)
.enter()
.append('h2')
.text(d => d + ' USD')
.style('color', d => (d < 20 ? 'red' : 'green'));
</script>
</body>