-Dynamically Set the Coordinates for Each Bar-

-Dynamically Set the Coordinates for Each Bar-
0

#1

Tell us what’s happening:
Where is my wrong??Can you help me?

Your code so far


<body>
  <script>
    const dataset = [12, 31, 22, 17, 25, 18, 29, 14, 9];
    
    const w = 500;
    const h = 100;
    
    const svg = d3.select("body")
                  .append("svg")
                  .attr("width", w)
                  .attr("height", h);
    
    svg.selectAll("rect")
       .data(dataset)
       .enter()
       .append("rect")
       .attr("x", (d, i) => {
         // Add your code below this line
       
            i = 0;          
            i += 30;       
        
         
         
         // Add your code above this line
       })
       .attr("y", 0)
       .attr("width", 25)
       .attr("height", 100);
  </script>
</body>

Your browser information:

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

Link to the challenge:
https://learn.freecodecamp.org/data-visualization/data-visualization-with-d3/dynamically-set-the-coordinates-for-each-bar


#2

Check your code, you are initializing i as 0 every time it runs, also think about this if you add i+30 where “i” is the index of your array, you will end up with if i =0, i+30=30, then i=1, i+30=31 and so on, so you must use a different operator than +. I think you should try a few more times on this to understand logic and then you will have your solution. Goodluck!


#3

Sorry but I can’t understand something.Can you explain me more easily?


#4

Compare your above code to below one, and it was a callback function so you had to use “return” to send a value back and “i” represents index of arrays so you had to muliply it by 30.


#5

Ohh yes now i got it thank you for the explain!