0

I could find some similar quesitons but I don't think the similar questions answers my question.

I made variables

line1 =[]
line2= []
line3 = []
line4 =[]

and I made a function drawline()

I would like to do looping like

for (let i = 1; i<5; i++){
drawline(`line${i}`)
}

However, there was an error message saying 'line' wasn't defined. I think I understood template literal, but it tells me that I don't understand it fully.

Can anyone help me?

Soonk
  • 332
  • 2
  • 14
  • Use an array of arrays instead – CertainPerformance Mar 23 '20 at 05:36
  • Thank you, You meant it's not template literal? – Soonk Mar 23 '20 at 05:38
  • 1
    You can't evaluate variables from strings with template literal. What you have, is a template literal, but you should not use it like that. Use nested arrays instead. – Teemu Mar 23 '20 at 05:43
  • so it's going to be like, for(let i=0;i<4;i++){ drawline(line[i]) } am i right? if not, please correct me. – Soonk Mar 23 '20 at 05:51
  • 2
    Yes, and `line` should be like `const line = [[/* the content of line1 */], [/* the content of line2 */], ... , [/* the content of lineN */]]`. – Teemu Mar 23 '20 at 05:59
  • Thank you. If you don't mind, one more question. what is /* */? – Soonk Mar 23 '20 at 06:05
  • ?? It's a JS comment, it's just a placeholder for the content you really have in the array variables in your example. – Teemu Mar 23 '20 at 07:50
  • @Teemu lol got ya, I haven't look into the shape of comment out. I just unconsciously press command slash. Thanks – Soonk Mar 25 '20 at 03:29

0 Answers0