Correctly solved the task of creating a chess Board in console?

As part of my course. There homework assignment was the task of creating a chess Board in console.
I tried to solve it. She sort of fulfills correctly, but the solution still seems not quite right. How to rewrite the following solution? You should pay attention to? Still want to solve the problem correctly.
createChess function(a, b) {
 var value1 = "# ";
 var value2 = " #";
 var odd = "";
 var even = "";
 for (var i = 0; i < a / 2; i += 1) {
 odd += value2;
 even += value1;
}

 for (var j = 0; j < b; j += 1) {
 if (j % 2 != 0) {
console.log(odd);
 } else {
console.log(even);
}
}
}

createChess(16, 16);


Input for example 8, 8 and get a Board size of 8x8. and it looks like this:
# # # #
 # # # #
# # # #
 # # # #
# # # #
 # # # #
# # # #
 # # # #


Update/

Slightly rewrote the solution. But XS better whether it is first or not. In General, similar solutions, but still, how correctly and effectively to solve this problem?

A slightly different solution:
createChess function(a, b, param1, param2) {
 var value1 = param1 || "#",
 value2 = param2 || " ";
 var odd = "",
 even = "";
 for (var i = 1; i <= b; i += 1) {
 if (i % 2 != 0) {
 for (var j = 0; j < a / 2; j += 1) {
 odd += value1 + value2;
}

console.log(odd);
 odd = "";
 } else {
 for (var k = 0; k < a / 2; k += 1) {
 even += value2 + value1;
}

console.log(even);
 even = "";
}
}
}

createChess(8, 8, "*", " ");


The second decision now seems better than the first. As each line again re-creates a row and think it's not right and it is inefficient. XS in General. Help to solve correctly

PSS.
Tried it to solve using just loops. It turned out more or less the option that I was more than satisfied.

createChessBoard function(value) {
 var board = "";
 for (var i = 1; i < value*value; i += 1) {
 if ((i % (value + 1)) == 0) {
 board += "\n";
 } else if (i % 2 != 0) {
 board += "*";
 } else {
 board += " ";
}
}

 return board;
}

console.log(createChessBoard(8));
July 8th 19 at 15:35
3 answers
July 8th 19 at 15:37
Sad:

createChess function(x, y) {
 for (var i = 0; i < y; i++) {
 var string = ", a, b;

 if (i % 2) {
 a = '#';
 b = ' ';
 } else {
 a = ' ';
 b = '#';
}

 for (var j = 0; j < x; j++) {
 if (j % 2) {
 string += a;
 } else {
 string += b;
}
}

console.log(string);
}
}

Hardcore:

createChess function(x, y) {
 for (var i = 0; i < y; i++) console.log((i % 2 ? '#' : '# ').repeat(Math.floor(x / 2) + 1).slice(0, x % 2 - 2));
}
July 8th 19 at 15:39
createChess function(width, height) {
 var pattern = '# '.repeat(Math.floor(width / 2) + 1 );
 var odd = pattern.substr(0, width);
 var even = pattern.substr(1, width + 1);
 var half_height = Math.floor(height / 2);
 for (var i = 0; i < half_height; i++) {
console.log(odd);
console.log(even);
}
 if ((height % 2) == 1) {
console.log(odd);
}
}
createChess(8,8);
The solution seems normal, but the methods of the Math object, I know, but the courses were only loops and functions. to objects not yet realized.
So you need to decide the subject studied. Namely, functions and cycles.
And so would about would as you suggested. - Howell commented on July 8th 19 at 15:42
: Then you are sort of right, only the odd and even rows can be done in one cycle. - Violet_Wiso commented on July 8th 19 at 15:45
July 8th 19 at 15:41
instead i+=1 always write i++
In the book of Stoyan Stefanov is recommended to write i += 1 instead of i++. And many developers recommend to write it as I wrote.
Explain your advice please. - Howell commented on July 8th 19 at 15:44

Find more questions by tags JavaScript