---
id: 587d7fa7367417b2b2512bc5
title: Work with Dynamic Data in D3
required:
- src: 'https://cdnjs.cloudflare.com/ajax/libs/d3/4.3.0/d3.min.js'
challengeType: 6
---
## Description
The last two challenges cover the basics of displaying data dynamically with D3 using the data()
and enter()
methods. These methods take a data set and, together with the append()
method, create a new DOM element for each entry in the data set.
In the previous challenge, you created a new h2
element for each item in the dataset
array, but they all contained the same text, "New Title". This is because you have not made use of the data that is bound to each of the h2
elements.
The D3 text()
method can take a string or a callback function as an argument:
selection.text((d) => d)
In the example above, the parameter d
refers to a single entry in the dataset that a selection is bound to.
Using the current example as context, the first h2
element is bound to 12, the second h2
element is bound to 31, the third h2
element is bound to 22, and so on.
## Instructions
Change the text()
method so that each h2
element displays the corresponding value from the dataset
array with a single space and "USD". For example, the first heading should be "12 USD".
## Tests
```yml
tests:
- text: The first h2
should have the text "12 USD".
testString: assert($('h2').eq(0).text() == "12 USD", 'The first h2
should have the text "12 USD".');
- text: The second h2
should have the text "31 USD".
testString: assert($('h2').eq(1).text() == "31 USD", 'The second h2
should have the text "31 USD".');
- text: The third h2
should have the text "22 USD".
testString: assert($('h2').eq(2).text() == "22 USD", 'The third h2
should have the text "22 USD".');
- text: The fourth h2
should have the text "17 USD".
testString: assert($('h2').eq(3).text() == "17 USD", 'The fourth h2
should have the text "17 USD".');
- text: The fifth h2
should have the text "25 USD".
testString: assert($('h2').eq(4).text() == "25 USD", 'The fifth h2
should have the text "25 USD".');
- text: The sixth h2
should have the text "18 USD".
testString: assert($('h2').eq(5).text() == "18 USD", 'The sixth h2
should have the text "18 USD".');
- text: The seventh h2
should have the text "29 USD".
testString: assert($('h2').eq(6).text() == "29 USD", 'The seventh h2
should have the text "29 USD".');
- text: The eighth h2
should have the text "14 USD".
testString: assert($('h2').eq(7).text() == "14 USD", 'The eighth h2
should have the text "14 USD".');
- text: The ninth h2
should have the text "9 USD".
testString: assert($('h2').eq(8).text() == "9 USD", 'The ninth h2
should have the text "9 USD".');
```
## Challenge Seed
## Solution
```js
// solution required
```