Help me change this code in for loop

Hello i would like to change this code in a for loop to reduce the code. The array goes until index 40…

var data = google.visualization.arrayToDataTable([
['datum', 'temperatuur'],
[time[0], parseFloat(temp[0])],
[time[1], parseFloat(temp[1])],
[time[2], parseFloat(temp[2])],
[time[3], parseFloat(temp[3])],
[time[4], parseFloat(temp[4])],
[time[5], parseFloat(temp[5])] ...[40]
]);

Hello there,

While we are primarily here to help people with their Free Code Camp progress, we are open to people on other paths, too. Some of what you are asking is pretty trivial in the Free Code Camp context, so you might find that if you’re not getting the instruction and material you need in your current studies, the FCC curriculum will really help you get started. At a modest guess I’d say investing a 4-5 hours working through the curriculum here will really pay off. You can find the curriculum at https://www.freecodecamp.org/learn.

With your current questions, we don’t have enough context to know what you already know or don’t know, so it is impossible to guide you without just telling you the answer (which we won’t do).

It is pretty typical on here for people to share a codepen / repl.it / jsfiddle example of what they have tried so that anyone helping has more of an idea of what help is actually helpful.

Please provide some example of what you’ve tried and I’m sure you’ll get more help.

Happy coding :slight_smile:

This is kinda what i tried. But this for loop only gives me the last index data in my chart. I’m new to javascript/php.

google.charts.load('current', {'packages':['corechart']});
google.charts.setOnLoadCallback(drawChart);
function drawChart() 
{
    var time =[], temp=[];
    for (i=0;i<40;i++)
        {
            time.push(document.getElementById("datatime["+i+"]").textContent);
            temp.push(document.getElementById("datatemp["+i+"]").textContent)
        }
        for (i=0;i<40;i++)
        {
            var data = google.visualization.arrayToDataTable([
                ['datum', 'temperatuur'],
                [time[i],parseFloat(temp[i])]
            ]);
        }
          var options = {'title':'temperatuur per tijdstip', 'width':1500, 'height':700};

          var chart = new google.visualization.ColumnChart(document.getElementById('barchart'));
          chart.draw(data, options);
    
}

That is not a bad attempt. Some things to try:

  1. You have 2 loops going through the same number of iterations. Combine the code so that you only have one of them.
  2. What would be easiest, in this case is for you to loop through (as you have), and create a new array containing just the [time[i],parseFloat(temp[i])] arrays.
    Then, add them to your data variable (outside of the loop).

The most useful method you will want to use for this is this: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/push

Go from there. If you get stuck, show what you have so far, and we can move from there.