Home > Enterprise >  A for loop with a string problem inside a var
A for loop with a string problem inside a var

Time:11-20

'''

How can I create a loop to remplace this code in javascript :

I do not know why the loop give me an error.

Can you help me?

Thank you.



var data1 = json2[(Math.floor(numero[1]*24))];
var data2 = json2[(Math.floor(numero[2]*24))];
var data3 = json2[(Math.floor(numero[3]*24))];
var data4 = json2[(Math.floor(numero[4]*24))];
var data5 = json2[(Math.floor(numero[5]*24))];

document.getElementById("wordi1").innerHTML = data1;
document.getElementById("wordi2").innerHTML = data2;
document.getElementById("wordi3").innerHTML = data3;
document.getElementById("wordi4").innerHTML = data4;
document.getElementById("wordi5").innerHTML = data5;

//This loop does not work
 for (let i = 1; i < 7; i  ) {
var data i = json2[(Math.floor(numero[i]*24))];
document.getElementById("wordi" i).innerHTML = data i ;}


CodePudding user response:

Try this

const dataArr = [];

for (let i = 1; i < 7; i  ) {
   const data = json2[(Math.floor(numero[i]*24))];
   document.getElementById("wordi" i).innerHTML = data;
   dataArr.push(data); 
}

console.log(dataArr); // this should have all your data values
  • Related