There is a code:

var string = ''; for (var i = 0;i<65;i++) { if (//тут для каждого 8го) {} string += '\n\t'; } string += '# '; } 

When running for every eighth, string += '\n\t'; How can this be implemented?

UPD:

 var size = 8; var board = ""; for (var y = 0; y < size; y++) { for (var x = 0; x < size; x++) { if ((x + y) % 2 == 0) board += " "; else board += "#"; } board += "\n"; } console.log(board); 

So it should have come to me, but it did not)

  • The second block of code is not related to the question. What is it all about? - Dmitriy Simushev
  • @DmitriySimushev Well, I tried to achieve this behavior. He added that the guy below would look - Nikita Shchypylov
  • ru.SO is not your personal counseling center. The second block of code is not related to the text of the question. Totally. - Dmitriy Simushev

2 answers 2

We check the remainder of dividing the counter by eight, every eighth of it will be the same. Since the cycle is from zero, it will be seven.

 var string = ''; for (var i = 0; i < 64; i++) { if (i%8 === 7) { // оператор % - взятие остатка string += '\n\t'; } string += '# '; } 

By the way, I assume that the condition was intended as i<64 , and not i<65

  • Perhaps) Thanks for the help - Nikita Shchypylov
  • It didn't work, gives everyone this code - Nikita Shchypylov
  • @ Nikita Schipilov, what exactly didn’t work?) Want a flat square - write i% 8 === 0 (but this isn’t quite "for every eighth") - Duck Learns to Take Cover
  • Look at the code I added to the question, pliz. How he works, I do not fully understand - Nikita Shchypylov
  • 2
    @ Nikita Schipilov, no, this will not work. This is more like "I have written the code on a different form here, explain it plz". Let's get specific questions that show what you were trying to figure out. - Duck Learns to Take Cover
 var string = ''; for (var i = 0; i < 64; i+=8) { string += '\n\t########'; } 

From the category of "what is the question"