Use Dynamic Scales

Tell us what’s happening:
why it return NaN?

Your code so far


<body>
  <script>
    const dataset = [
                  [ 34,    78 ],
                  [ 109,   280 ],
                  [ 310,   120 ],
                  [ 79,    411 ],
                  [ 420,   220 ],
                  [ 233,   145 ],
                  [ 333,   96 ],
                  [ 222,   333 ],
                  [ 78,    320 ],
                  [ 21,    123 ]
                ];
    
    const w = 500;
    const h = 500;
    
    // Padding between the SVG canvas boundary and the plot
    const padding = 30;
    
    // Create an x and y scale
    
    const xScale = d3.scaleLinear()
                    .domain([0, d3.max(dataset, (d) => d[0])])
                    .range([padding, w - padding]);
    
    // Add your code below this line
    
    const yScale = d3.scaleLinear()
              .domain([0,d3.max(dataset,(d)=>d[3])])
                    .range([w-padding,padding]);
                     
                     
    // Add your code above this line
    
    const output = yScale(411); // Returns 30
    d3.select("body")
      .append("h2")
      .text(output)
  </script>
</body>

Your browser information:

User Agent is: Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/67.0.3396.99 Safari/537.36.

Link to the challenge:
https://learn.freecodecamp.org/data-visualization/data-visualization-with-d3/use-dynamic-scales

Shouldn’t you use d[1]?

NaN happens because there’s no data in d[3]. There would only be 2 numbers in any of those arrays.

Also, the Y access corresponds with height, not width. Those notes might help solve it!

1 Like
    const yScale = d3.scaleLinear()
    .domain([0, d3.max(dataset, (d) => d[1])])
    .range([h - padding, padding])

and that’s it!

1 Like