Better solution for "Update the height of an element dynamically" from D3 challenges

Better solution for "Update the height of an element dynamically" from D3 challenges
0

The forum post containing the solutions to this challenge is not replyable or editable: freeCodeCamp Challenge Guide: Update the Height of an Element Dynamically

So I am making a new topic. The solution as presented in forum post uses template strings:

.style('height', d => ${d}px)

But the challenge instructions do not mention the use of template strings, nor do any of the previous challenges. It works, but feels disconnected to the challenge. How would the user know to use a template string / string literal here?

Here is my solution that completes the challenge, and is more consistent with the instructions in this challenge and previous ones:

.style("height", (d) => d + "px");

Here is the entire code for my solution:

<style>
  .bar {
    width: 25px;
    height: 100px;
    display: inline-block;
    background-color: blue;
  }
</style>
<body>
  <script>
    const dataset = [12, 31, 22, 17, 25, 18, 29, 14, 9];

    d3.select("body").selectAll("div")
      .data(dataset)
      .enter()
      .append("div")
      .attr("class", "bar")
      // Add your code below this line

      .style("height", (d) => d + "px");

      // Add your code above this line
  </script>
</body>

The instructions do not say you have to use a template literal, because you do not have to in order to solve the problem. That being said, users would have already learned about template literals in a preceding JavaScript section of the curriculum, so the solution seen in the guide should not confuse them.

If a user skips to the subsequent challenge it has a callback function written in the sample code. That tells me a callback function is what the author of the challenge intended.

I propose we add a second solution that includes a callback function.

The current solution does use a callback function (see below):

d => `${d}px`

That is a callback function, just like yours uses a callback function. The only difference is the template literal (current) vs. string concatenation (yours).

Okay, it sounds like you are satisfied with the 1 solution, so I won’t spend any more of your time on this. Have a good one :+1: