Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

A for loop with a string problem inside a var

”’

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

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

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

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 ;}


>Solution :

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
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading