Chessboard in pure js
up vote
8
down vote
favorite
I began js 3 days ago at school, I had some C basics before so the main problem here would be the syntax (I think).
The goal is to produce a chessboard, 8x8 black and white square, but I can't get the code to show anything. What am I missing ?
(the html just have the script src="./x.js" part and the "body" part)
document.body.onload = addElement.innerHTML;
function addElement() {
var newTable = document.createElement("table");
for (var i = 1; i < 9; i++) {
var newTr = document.createElement('tr');
for (var j = 1; j < 9; j++) {
var newTd = document.createElement('td');
if (i % 2 == j % 2) {
newTd.className = "white";
} else {
newTd.className = "black";
}
newTr.appendChild(newTd);
}
newTable.appendChild(newTr);
}
document.body.appendChild(newTable);
document.getElementByClass("black").style.backgroundColor = "black";
document.getElementByClass("white").style.backgroundColor = "white";
document.getElementByTag("newTd").style.width = "25px";
document.getElementByTag("newTd").style.height = "25px";
}
javascript
New contributor
add a comment |
up vote
8
down vote
favorite
I began js 3 days ago at school, I had some C basics before so the main problem here would be the syntax (I think).
The goal is to produce a chessboard, 8x8 black and white square, but I can't get the code to show anything. What am I missing ?
(the html just have the script src="./x.js" part and the "body" part)
document.body.onload = addElement.innerHTML;
function addElement() {
var newTable = document.createElement("table");
for (var i = 1; i < 9; i++) {
var newTr = document.createElement('tr');
for (var j = 1; j < 9; j++) {
var newTd = document.createElement('td');
if (i % 2 == j % 2) {
newTd.className = "white";
} else {
newTd.className = "black";
}
newTr.appendChild(newTd);
}
newTable.appendChild(newTr);
}
document.body.appendChild(newTable);
document.getElementByClass("black").style.backgroundColor = "black";
document.getElementByClass("white").style.backgroundColor = "white";
document.getElementByTag("newTd").style.width = "25px";
document.getElementByTag("newTd").style.height = "25px";
}
javascript
New contributor
9
Quick tip - press F12 and you'll be able to see any errors in your console.
– markmoxx
5 hours ago
add a comment |
up vote
8
down vote
favorite
up vote
8
down vote
favorite
I began js 3 days ago at school, I had some C basics before so the main problem here would be the syntax (I think).
The goal is to produce a chessboard, 8x8 black and white square, but I can't get the code to show anything. What am I missing ?
(the html just have the script src="./x.js" part and the "body" part)
document.body.onload = addElement.innerHTML;
function addElement() {
var newTable = document.createElement("table");
for (var i = 1; i < 9; i++) {
var newTr = document.createElement('tr');
for (var j = 1; j < 9; j++) {
var newTd = document.createElement('td');
if (i % 2 == j % 2) {
newTd.className = "white";
} else {
newTd.className = "black";
}
newTr.appendChild(newTd);
}
newTable.appendChild(newTr);
}
document.body.appendChild(newTable);
document.getElementByClass("black").style.backgroundColor = "black";
document.getElementByClass("white").style.backgroundColor = "white";
document.getElementByTag("newTd").style.width = "25px";
document.getElementByTag("newTd").style.height = "25px";
}
javascript
New contributor
I began js 3 days ago at school, I had some C basics before so the main problem here would be the syntax (I think).
The goal is to produce a chessboard, 8x8 black and white square, but I can't get the code to show anything. What am I missing ?
(the html just have the script src="./x.js" part and the "body" part)
document.body.onload = addElement.innerHTML;
function addElement() {
var newTable = document.createElement("table");
for (var i = 1; i < 9; i++) {
var newTr = document.createElement('tr');
for (var j = 1; j < 9; j++) {
var newTd = document.createElement('td');
if (i % 2 == j % 2) {
newTd.className = "white";
} else {
newTd.className = "black";
}
newTr.appendChild(newTd);
}
newTable.appendChild(newTr);
}
document.body.appendChild(newTable);
document.getElementByClass("black").style.backgroundColor = "black";
document.getElementByClass("white").style.backgroundColor = "white";
document.getElementByTag("newTd").style.width = "25px";
document.getElementByTag("newTd").style.height = "25px";
}
javascript
javascript
New contributor
New contributor
edited 5 hours ago
markmoxx
579213
579213
New contributor
asked 5 hours ago
Jaune
463
463
New contributor
New contributor
9
Quick tip - press F12 and you'll be able to see any errors in your console.
– markmoxx
5 hours ago
add a comment |
9
Quick tip - press F12 and you'll be able to see any errors in your console.
– markmoxx
5 hours ago
9
9
Quick tip - press F12 and you'll be able to see any errors in your console.
– markmoxx
5 hours ago
Quick tip - press F12 and you'll be able to see any errors in your console.
– markmoxx
5 hours ago
add a comment |
4 Answers
4
active
oldest
votes
up vote
7
down vote
accepted
There are few mistakes in your code use getElementsByClassName
instead of getElementByClass
and use getElementsByTagName
instead of getElementByTag
.
Also you need to loop over each selected elements.
Use window.onload = addElement;
or you can simply call addElement();
after function declaration completes.
window.onload = addElement;
function addElement() {
var newTable = document.createElement("table");
for (var i = 1; i < 9; i++) {
var newTr = document.createElement('tr');
for (var j = 1; j < 9; j++) {
var newTd = document.createElement('td');
if (i % 2 == j % 2) {
newTd.className = "white";
} else {
newTd.className = "black";
}
newTr.appendChild(newTd);
}
newTable.appendChild(newTr);
}
document.body.appendChild(newTable);
var i = 0;
for (i = 0; i < document.getElementsByClassName("black").length; i++) {
document.getElementsByClassName("black")[i].style.backgroundColor = "black";
}
for (i = 0; i < document.getElementsByClassName("white").length; i++) {
document.getElementsByClassName("white")[i].style.backgroundColor = "white";
}
for (i = 0; i < document.getElementsByTagName("td").length; i++) {
document.getElementsByTagName("td")[i].style.width = "25px";
document.getElementsByTagName("td")[i].style.height = "25px";
}
}
Perfect ! Thanks a lot, it solves everything.
– Jaune
5 hours ago
add a comment |
up vote
3
down vote
As you currently have it, addElement.innerHTML
is a reference to the innerHTML property of the addElement
variable. addElement
is a reference to a function definition, and does not have an innerHTML
property.
Try changing your first line to this:
window.onload = addElement;
See: https://developer.mozilla.org/en-US/docs/Web/API/GlobalEventHandlers/onload
Edit: I was a bit too quick to post this, and didn't spot the other errors in the code as mentioned in Karan's answer.
1
There is no explanation what so ever, he is curious about knowing what he did wrong, the solution might be useful, but seems less of a priority / necessity
– Icepickle
5 hours ago
Oops, thanks @RolandStarke
– markmoxx
5 hours ago
Well indeed it doesn't solve my problem, but I'm learning so I'm still happy to understand something better so thank anyway
– Jaune
5 hours ago
add a comment |
up vote
3
down vote
A simpler approach would be to add the attributes when you create each element and not after. Like this:
if (i % 2 == j % 2) newTd.style.backgroundColor = "white";
else newTd.style.backgroundColor = "black";
newTd.style.width = "25px";
newTd.style.height = "25px";
See this fiddle: http://jsfiddle.net/vdquLn65/2/
About your current code you could use getElementsByClassName and getElementsByTagName and iterate over the elements they return to do what you want.
Yeah I thought about that too while making the tests, but I opted in the current organisation so the css is all in the same spot, after the main js part. It could have made it easier to solve my problem tho, or even solve it. Thanks for the advice
– Jaune
5 hours ago
add a comment |
up vote
1
down vote
Hello Jaune and welcome to Stack Overflow. I have added an answer that will create the data first and then map that that data to elements using functions. If you have any questions please let me know.
const field = ([x, y, color]) => {
const td = document.createElement('td');
td.setAttribute('data-x', x);
td.setAttribute('data-y', y);
td.className = color;
td.innerHtml = `${x}-${y}`;
return td;
};
const row = (row) => {
const tr = document.createElement('tr');
row.forEach((fieldItem) =>
tr.appendChild(field(fieldItem)),
);
return tr;
};
const table = (tableData) => {
const table = document.createElement('table');
tableData.forEach((rowData) =>
table.appendChild(row(rowData)),
);
return table;
};
const tableData = Array.from(Array(8).keys()).map((x) =>
Array.from(Array(8).keys()).map((y) => [
x,
y,
(x + y) % 2 ? 'black' : 'white',
]),
);
document.body.appendChild(table(tableData));
.black {
width:10px;
height:10px;
background-color: black;
}
.white {
width:10px;
height:10px;
background-color: white;
}
table {
border: 1px solid black;
}
Hello HMR, and thanks for the warm welcome. I'd gladly take your proposal for help, but I'm still a little too fresh with the syntax to have interesting question about your code at the moment. Thanks again tho
– Jaune
4 hours ago
1
@Jaune The following book can help you out with syntax and here are some handy array functions.
– HMR
4 hours ago
1
Great docs, thanks a lot !
– Jaune
3 hours ago
add a comment |
4 Answers
4
active
oldest
votes
4 Answers
4
active
oldest
votes
active
oldest
votes
active
oldest
votes
up vote
7
down vote
accepted
There are few mistakes in your code use getElementsByClassName
instead of getElementByClass
and use getElementsByTagName
instead of getElementByTag
.
Also you need to loop over each selected elements.
Use window.onload = addElement;
or you can simply call addElement();
after function declaration completes.
window.onload = addElement;
function addElement() {
var newTable = document.createElement("table");
for (var i = 1; i < 9; i++) {
var newTr = document.createElement('tr');
for (var j = 1; j < 9; j++) {
var newTd = document.createElement('td');
if (i % 2 == j % 2) {
newTd.className = "white";
} else {
newTd.className = "black";
}
newTr.appendChild(newTd);
}
newTable.appendChild(newTr);
}
document.body.appendChild(newTable);
var i = 0;
for (i = 0; i < document.getElementsByClassName("black").length; i++) {
document.getElementsByClassName("black")[i].style.backgroundColor = "black";
}
for (i = 0; i < document.getElementsByClassName("white").length; i++) {
document.getElementsByClassName("white")[i].style.backgroundColor = "white";
}
for (i = 0; i < document.getElementsByTagName("td").length; i++) {
document.getElementsByTagName("td")[i].style.width = "25px";
document.getElementsByTagName("td")[i].style.height = "25px";
}
}
Perfect ! Thanks a lot, it solves everything.
– Jaune
5 hours ago
add a comment |
up vote
7
down vote
accepted
There are few mistakes in your code use getElementsByClassName
instead of getElementByClass
and use getElementsByTagName
instead of getElementByTag
.
Also you need to loop over each selected elements.
Use window.onload = addElement;
or you can simply call addElement();
after function declaration completes.
window.onload = addElement;
function addElement() {
var newTable = document.createElement("table");
for (var i = 1; i < 9; i++) {
var newTr = document.createElement('tr');
for (var j = 1; j < 9; j++) {
var newTd = document.createElement('td');
if (i % 2 == j % 2) {
newTd.className = "white";
} else {
newTd.className = "black";
}
newTr.appendChild(newTd);
}
newTable.appendChild(newTr);
}
document.body.appendChild(newTable);
var i = 0;
for (i = 0; i < document.getElementsByClassName("black").length; i++) {
document.getElementsByClassName("black")[i].style.backgroundColor = "black";
}
for (i = 0; i < document.getElementsByClassName("white").length; i++) {
document.getElementsByClassName("white")[i].style.backgroundColor = "white";
}
for (i = 0; i < document.getElementsByTagName("td").length; i++) {
document.getElementsByTagName("td")[i].style.width = "25px";
document.getElementsByTagName("td")[i].style.height = "25px";
}
}
Perfect ! Thanks a lot, it solves everything.
– Jaune
5 hours ago
add a comment |
up vote
7
down vote
accepted
up vote
7
down vote
accepted
There are few mistakes in your code use getElementsByClassName
instead of getElementByClass
and use getElementsByTagName
instead of getElementByTag
.
Also you need to loop over each selected elements.
Use window.onload = addElement;
or you can simply call addElement();
after function declaration completes.
window.onload = addElement;
function addElement() {
var newTable = document.createElement("table");
for (var i = 1; i < 9; i++) {
var newTr = document.createElement('tr');
for (var j = 1; j < 9; j++) {
var newTd = document.createElement('td');
if (i % 2 == j % 2) {
newTd.className = "white";
} else {
newTd.className = "black";
}
newTr.appendChild(newTd);
}
newTable.appendChild(newTr);
}
document.body.appendChild(newTable);
var i = 0;
for (i = 0; i < document.getElementsByClassName("black").length; i++) {
document.getElementsByClassName("black")[i].style.backgroundColor = "black";
}
for (i = 0; i < document.getElementsByClassName("white").length; i++) {
document.getElementsByClassName("white")[i].style.backgroundColor = "white";
}
for (i = 0; i < document.getElementsByTagName("td").length; i++) {
document.getElementsByTagName("td")[i].style.width = "25px";
document.getElementsByTagName("td")[i].style.height = "25px";
}
}
There are few mistakes in your code use getElementsByClassName
instead of getElementByClass
and use getElementsByTagName
instead of getElementByTag
.
Also you need to loop over each selected elements.
Use window.onload = addElement;
or you can simply call addElement();
after function declaration completes.
window.onload = addElement;
function addElement() {
var newTable = document.createElement("table");
for (var i = 1; i < 9; i++) {
var newTr = document.createElement('tr');
for (var j = 1; j < 9; j++) {
var newTd = document.createElement('td');
if (i % 2 == j % 2) {
newTd.className = "white";
} else {
newTd.className = "black";
}
newTr.appendChild(newTd);
}
newTable.appendChild(newTr);
}
document.body.appendChild(newTable);
var i = 0;
for (i = 0; i < document.getElementsByClassName("black").length; i++) {
document.getElementsByClassName("black")[i].style.backgroundColor = "black";
}
for (i = 0; i < document.getElementsByClassName("white").length; i++) {
document.getElementsByClassName("white")[i].style.backgroundColor = "white";
}
for (i = 0; i < document.getElementsByTagName("td").length; i++) {
document.getElementsByTagName("td")[i].style.width = "25px";
document.getElementsByTagName("td")[i].style.height = "25px";
}
}
window.onload = addElement;
function addElement() {
var newTable = document.createElement("table");
for (var i = 1; i < 9; i++) {
var newTr = document.createElement('tr');
for (var j = 1; j < 9; j++) {
var newTd = document.createElement('td');
if (i % 2 == j % 2) {
newTd.className = "white";
} else {
newTd.className = "black";
}
newTr.appendChild(newTd);
}
newTable.appendChild(newTr);
}
document.body.appendChild(newTable);
var i = 0;
for (i = 0; i < document.getElementsByClassName("black").length; i++) {
document.getElementsByClassName("black")[i].style.backgroundColor = "black";
}
for (i = 0; i < document.getElementsByClassName("white").length; i++) {
document.getElementsByClassName("white")[i].style.backgroundColor = "white";
}
for (i = 0; i < document.getElementsByTagName("td").length; i++) {
document.getElementsByTagName("td")[i].style.width = "25px";
document.getElementsByTagName("td")[i].style.height = "25px";
}
}
window.onload = addElement;
function addElement() {
var newTable = document.createElement("table");
for (var i = 1; i < 9; i++) {
var newTr = document.createElement('tr');
for (var j = 1; j < 9; j++) {
var newTd = document.createElement('td');
if (i % 2 == j % 2) {
newTd.className = "white";
} else {
newTd.className = "black";
}
newTr.appendChild(newTd);
}
newTable.appendChild(newTr);
}
document.body.appendChild(newTable);
var i = 0;
for (i = 0; i < document.getElementsByClassName("black").length; i++) {
document.getElementsByClassName("black")[i].style.backgroundColor = "black";
}
for (i = 0; i < document.getElementsByClassName("white").length; i++) {
document.getElementsByClassName("white")[i].style.backgroundColor = "white";
}
for (i = 0; i < document.getElementsByTagName("td").length; i++) {
document.getElementsByTagName("td")[i].style.width = "25px";
document.getElementsByTagName("td")[i].style.height = "25px";
}
}
answered 5 hours ago
Karan
2,7252322
2,7252322
Perfect ! Thanks a lot, it solves everything.
– Jaune
5 hours ago
add a comment |
Perfect ! Thanks a lot, it solves everything.
– Jaune
5 hours ago
Perfect ! Thanks a lot, it solves everything.
– Jaune
5 hours ago
Perfect ! Thanks a lot, it solves everything.
– Jaune
5 hours ago
add a comment |
up vote
3
down vote
As you currently have it, addElement.innerHTML
is a reference to the innerHTML property of the addElement
variable. addElement
is a reference to a function definition, and does not have an innerHTML
property.
Try changing your first line to this:
window.onload = addElement;
See: https://developer.mozilla.org/en-US/docs/Web/API/GlobalEventHandlers/onload
Edit: I was a bit too quick to post this, and didn't spot the other errors in the code as mentioned in Karan's answer.
1
There is no explanation what so ever, he is curious about knowing what he did wrong, the solution might be useful, but seems less of a priority / necessity
– Icepickle
5 hours ago
Oops, thanks @RolandStarke
– markmoxx
5 hours ago
Well indeed it doesn't solve my problem, but I'm learning so I'm still happy to understand something better so thank anyway
– Jaune
5 hours ago
add a comment |
up vote
3
down vote
As you currently have it, addElement.innerHTML
is a reference to the innerHTML property of the addElement
variable. addElement
is a reference to a function definition, and does not have an innerHTML
property.
Try changing your first line to this:
window.onload = addElement;
See: https://developer.mozilla.org/en-US/docs/Web/API/GlobalEventHandlers/onload
Edit: I was a bit too quick to post this, and didn't spot the other errors in the code as mentioned in Karan's answer.
1
There is no explanation what so ever, he is curious about knowing what he did wrong, the solution might be useful, but seems less of a priority / necessity
– Icepickle
5 hours ago
Oops, thanks @RolandStarke
– markmoxx
5 hours ago
Well indeed it doesn't solve my problem, but I'm learning so I'm still happy to understand something better so thank anyway
– Jaune
5 hours ago
add a comment |
up vote
3
down vote
up vote
3
down vote
As you currently have it, addElement.innerHTML
is a reference to the innerHTML property of the addElement
variable. addElement
is a reference to a function definition, and does not have an innerHTML
property.
Try changing your first line to this:
window.onload = addElement;
See: https://developer.mozilla.org/en-US/docs/Web/API/GlobalEventHandlers/onload
Edit: I was a bit too quick to post this, and didn't spot the other errors in the code as mentioned in Karan's answer.
As you currently have it, addElement.innerHTML
is a reference to the innerHTML property of the addElement
variable. addElement
is a reference to a function definition, and does not have an innerHTML
property.
Try changing your first line to this:
window.onload = addElement;
See: https://developer.mozilla.org/en-US/docs/Web/API/GlobalEventHandlers/onload
Edit: I was a bit too quick to post this, and didn't spot the other errors in the code as mentioned in Karan's answer.
edited 5 hours ago
answered 5 hours ago
markmoxx
579213
579213
1
There is no explanation what so ever, he is curious about knowing what he did wrong, the solution might be useful, but seems less of a priority / necessity
– Icepickle
5 hours ago
Oops, thanks @RolandStarke
– markmoxx
5 hours ago
Well indeed it doesn't solve my problem, but I'm learning so I'm still happy to understand something better so thank anyway
– Jaune
5 hours ago
add a comment |
1
There is no explanation what so ever, he is curious about knowing what he did wrong, the solution might be useful, but seems less of a priority / necessity
– Icepickle
5 hours ago
Oops, thanks @RolandStarke
– markmoxx
5 hours ago
Well indeed it doesn't solve my problem, but I'm learning so I'm still happy to understand something better so thank anyway
– Jaune
5 hours ago
1
1
There is no explanation what so ever, he is curious about knowing what he did wrong, the solution might be useful, but seems less of a priority / necessity
– Icepickle
5 hours ago
There is no explanation what so ever, he is curious about knowing what he did wrong, the solution might be useful, but seems less of a priority / necessity
– Icepickle
5 hours ago
Oops, thanks @RolandStarke
– markmoxx
5 hours ago
Oops, thanks @RolandStarke
– markmoxx
5 hours ago
Well indeed it doesn't solve my problem, but I'm learning so I'm still happy to understand something better so thank anyway
– Jaune
5 hours ago
Well indeed it doesn't solve my problem, but I'm learning so I'm still happy to understand something better so thank anyway
– Jaune
5 hours ago
add a comment |
up vote
3
down vote
A simpler approach would be to add the attributes when you create each element and not after. Like this:
if (i % 2 == j % 2) newTd.style.backgroundColor = "white";
else newTd.style.backgroundColor = "black";
newTd.style.width = "25px";
newTd.style.height = "25px";
See this fiddle: http://jsfiddle.net/vdquLn65/2/
About your current code you could use getElementsByClassName and getElementsByTagName and iterate over the elements they return to do what you want.
Yeah I thought about that too while making the tests, but I opted in the current organisation so the css is all in the same spot, after the main js part. It could have made it easier to solve my problem tho, or even solve it. Thanks for the advice
– Jaune
5 hours ago
add a comment |
up vote
3
down vote
A simpler approach would be to add the attributes when you create each element and not after. Like this:
if (i % 2 == j % 2) newTd.style.backgroundColor = "white";
else newTd.style.backgroundColor = "black";
newTd.style.width = "25px";
newTd.style.height = "25px";
See this fiddle: http://jsfiddle.net/vdquLn65/2/
About your current code you could use getElementsByClassName and getElementsByTagName and iterate over the elements they return to do what you want.
Yeah I thought about that too while making the tests, but I opted in the current organisation so the css is all in the same spot, after the main js part. It could have made it easier to solve my problem tho, or even solve it. Thanks for the advice
– Jaune
5 hours ago
add a comment |
up vote
3
down vote
up vote
3
down vote
A simpler approach would be to add the attributes when you create each element and not after. Like this:
if (i % 2 == j % 2) newTd.style.backgroundColor = "white";
else newTd.style.backgroundColor = "black";
newTd.style.width = "25px";
newTd.style.height = "25px";
See this fiddle: http://jsfiddle.net/vdquLn65/2/
About your current code you could use getElementsByClassName and getElementsByTagName and iterate over the elements they return to do what you want.
A simpler approach would be to add the attributes when you create each element and not after. Like this:
if (i % 2 == j % 2) newTd.style.backgroundColor = "white";
else newTd.style.backgroundColor = "black";
newTd.style.width = "25px";
newTd.style.height = "25px";
See this fiddle: http://jsfiddle.net/vdquLn65/2/
About your current code you could use getElementsByClassName and getElementsByTagName and iterate over the elements they return to do what you want.
answered 5 hours ago
Alex G
1,01529
1,01529
Yeah I thought about that too while making the tests, but I opted in the current organisation so the css is all in the same spot, after the main js part. It could have made it easier to solve my problem tho, or even solve it. Thanks for the advice
– Jaune
5 hours ago
add a comment |
Yeah I thought about that too while making the tests, but I opted in the current organisation so the css is all in the same spot, after the main js part. It could have made it easier to solve my problem tho, or even solve it. Thanks for the advice
– Jaune
5 hours ago
Yeah I thought about that too while making the tests, but I opted in the current organisation so the css is all in the same spot, after the main js part. It could have made it easier to solve my problem tho, or even solve it. Thanks for the advice
– Jaune
5 hours ago
Yeah I thought about that too while making the tests, but I opted in the current organisation so the css is all in the same spot, after the main js part. It could have made it easier to solve my problem tho, or even solve it. Thanks for the advice
– Jaune
5 hours ago
add a comment |
up vote
1
down vote
Hello Jaune and welcome to Stack Overflow. I have added an answer that will create the data first and then map that that data to elements using functions. If you have any questions please let me know.
const field = ([x, y, color]) => {
const td = document.createElement('td');
td.setAttribute('data-x', x);
td.setAttribute('data-y', y);
td.className = color;
td.innerHtml = `${x}-${y}`;
return td;
};
const row = (row) => {
const tr = document.createElement('tr');
row.forEach((fieldItem) =>
tr.appendChild(field(fieldItem)),
);
return tr;
};
const table = (tableData) => {
const table = document.createElement('table');
tableData.forEach((rowData) =>
table.appendChild(row(rowData)),
);
return table;
};
const tableData = Array.from(Array(8).keys()).map((x) =>
Array.from(Array(8).keys()).map((y) => [
x,
y,
(x + y) % 2 ? 'black' : 'white',
]),
);
document.body.appendChild(table(tableData));
.black {
width:10px;
height:10px;
background-color: black;
}
.white {
width:10px;
height:10px;
background-color: white;
}
table {
border: 1px solid black;
}
Hello HMR, and thanks for the warm welcome. I'd gladly take your proposal for help, but I'm still a little too fresh with the syntax to have interesting question about your code at the moment. Thanks again tho
– Jaune
4 hours ago
1
@Jaune The following book can help you out with syntax and here are some handy array functions.
– HMR
4 hours ago
1
Great docs, thanks a lot !
– Jaune
3 hours ago
add a comment |
up vote
1
down vote
Hello Jaune and welcome to Stack Overflow. I have added an answer that will create the data first and then map that that data to elements using functions. If you have any questions please let me know.
const field = ([x, y, color]) => {
const td = document.createElement('td');
td.setAttribute('data-x', x);
td.setAttribute('data-y', y);
td.className = color;
td.innerHtml = `${x}-${y}`;
return td;
};
const row = (row) => {
const tr = document.createElement('tr');
row.forEach((fieldItem) =>
tr.appendChild(field(fieldItem)),
);
return tr;
};
const table = (tableData) => {
const table = document.createElement('table');
tableData.forEach((rowData) =>
table.appendChild(row(rowData)),
);
return table;
};
const tableData = Array.from(Array(8).keys()).map((x) =>
Array.from(Array(8).keys()).map((y) => [
x,
y,
(x + y) % 2 ? 'black' : 'white',
]),
);
document.body.appendChild(table(tableData));
.black {
width:10px;
height:10px;
background-color: black;
}
.white {
width:10px;
height:10px;
background-color: white;
}
table {
border: 1px solid black;
}
Hello HMR, and thanks for the warm welcome. I'd gladly take your proposal for help, but I'm still a little too fresh with the syntax to have interesting question about your code at the moment. Thanks again tho
– Jaune
4 hours ago
1
@Jaune The following book can help you out with syntax and here are some handy array functions.
– HMR
4 hours ago
1
Great docs, thanks a lot !
– Jaune
3 hours ago
add a comment |
up vote
1
down vote
up vote
1
down vote
Hello Jaune and welcome to Stack Overflow. I have added an answer that will create the data first and then map that that data to elements using functions. If you have any questions please let me know.
const field = ([x, y, color]) => {
const td = document.createElement('td');
td.setAttribute('data-x', x);
td.setAttribute('data-y', y);
td.className = color;
td.innerHtml = `${x}-${y}`;
return td;
};
const row = (row) => {
const tr = document.createElement('tr');
row.forEach((fieldItem) =>
tr.appendChild(field(fieldItem)),
);
return tr;
};
const table = (tableData) => {
const table = document.createElement('table');
tableData.forEach((rowData) =>
table.appendChild(row(rowData)),
);
return table;
};
const tableData = Array.from(Array(8).keys()).map((x) =>
Array.from(Array(8).keys()).map((y) => [
x,
y,
(x + y) % 2 ? 'black' : 'white',
]),
);
document.body.appendChild(table(tableData));
.black {
width:10px;
height:10px;
background-color: black;
}
.white {
width:10px;
height:10px;
background-color: white;
}
table {
border: 1px solid black;
}
Hello Jaune and welcome to Stack Overflow. I have added an answer that will create the data first and then map that that data to elements using functions. If you have any questions please let me know.
const field = ([x, y, color]) => {
const td = document.createElement('td');
td.setAttribute('data-x', x);
td.setAttribute('data-y', y);
td.className = color;
td.innerHtml = `${x}-${y}`;
return td;
};
const row = (row) => {
const tr = document.createElement('tr');
row.forEach((fieldItem) =>
tr.appendChild(field(fieldItem)),
);
return tr;
};
const table = (tableData) => {
const table = document.createElement('table');
tableData.forEach((rowData) =>
table.appendChild(row(rowData)),
);
return table;
};
const tableData = Array.from(Array(8).keys()).map((x) =>
Array.from(Array(8).keys()).map((y) => [
x,
y,
(x + y) % 2 ? 'black' : 'white',
]),
);
document.body.appendChild(table(tableData));
.black {
width:10px;
height:10px;
background-color: black;
}
.white {
width:10px;
height:10px;
background-color: white;
}
table {
border: 1px solid black;
}
const field = ([x, y, color]) => {
const td = document.createElement('td');
td.setAttribute('data-x', x);
td.setAttribute('data-y', y);
td.className = color;
td.innerHtml = `${x}-${y}`;
return td;
};
const row = (row) => {
const tr = document.createElement('tr');
row.forEach((fieldItem) =>
tr.appendChild(field(fieldItem)),
);
return tr;
};
const table = (tableData) => {
const table = document.createElement('table');
tableData.forEach((rowData) =>
table.appendChild(row(rowData)),
);
return table;
};
const tableData = Array.from(Array(8).keys()).map((x) =>
Array.from(Array(8).keys()).map((y) => [
x,
y,
(x + y) % 2 ? 'black' : 'white',
]),
);
document.body.appendChild(table(tableData));
.black {
width:10px;
height:10px;
background-color: black;
}
.white {
width:10px;
height:10px;
background-color: white;
}
table {
border: 1px solid black;
}
const field = ([x, y, color]) => {
const td = document.createElement('td');
td.setAttribute('data-x', x);
td.setAttribute('data-y', y);
td.className = color;
td.innerHtml = `${x}-${y}`;
return td;
};
const row = (row) => {
const tr = document.createElement('tr');
row.forEach((fieldItem) =>
tr.appendChild(field(fieldItem)),
);
return tr;
};
const table = (tableData) => {
const table = document.createElement('table');
tableData.forEach((rowData) =>
table.appendChild(row(rowData)),
);
return table;
};
const tableData = Array.from(Array(8).keys()).map((x) =>
Array.from(Array(8).keys()).map((y) => [
x,
y,
(x + y) % 2 ? 'black' : 'white',
]),
);
document.body.appendChild(table(tableData));
.black {
width:10px;
height:10px;
background-color: black;
}
.white {
width:10px;
height:10px;
background-color: white;
}
table {
border: 1px solid black;
}
answered 5 hours ago
HMR
13.3k113898
13.3k113898
Hello HMR, and thanks for the warm welcome. I'd gladly take your proposal for help, but I'm still a little too fresh with the syntax to have interesting question about your code at the moment. Thanks again tho
– Jaune
4 hours ago
1
@Jaune The following book can help you out with syntax and here are some handy array functions.
– HMR
4 hours ago
1
Great docs, thanks a lot !
– Jaune
3 hours ago
add a comment |
Hello HMR, and thanks for the warm welcome. I'd gladly take your proposal for help, but I'm still a little too fresh with the syntax to have interesting question about your code at the moment. Thanks again tho
– Jaune
4 hours ago
1
@Jaune The following book can help you out with syntax and here are some handy array functions.
– HMR
4 hours ago
1
Great docs, thanks a lot !
– Jaune
3 hours ago
Hello HMR, and thanks for the warm welcome. I'd gladly take your proposal for help, but I'm still a little too fresh with the syntax to have interesting question about your code at the moment. Thanks again tho
– Jaune
4 hours ago
Hello HMR, and thanks for the warm welcome. I'd gladly take your proposal for help, but I'm still a little too fresh with the syntax to have interesting question about your code at the moment. Thanks again tho
– Jaune
4 hours ago
1
1
@Jaune The following book can help you out with syntax and here are some handy array functions.
– HMR
4 hours ago
@Jaune The following book can help you out with syntax and here are some handy array functions.
– HMR
4 hours ago
1
1
Great docs, thanks a lot !
– Jaune
3 hours ago
Great docs, thanks a lot !
– Jaune
3 hours ago
add a comment |
Jaune is a new contributor. Be nice, and check out our Code of Conduct.
Jaune is a new contributor. Be nice, and check out our Code of Conduct.
Jaune is a new contributor. Be nice, and check out our Code of Conduct.
Jaune is a new contributor. Be nice, and check out our Code of Conduct.
Thanks for contributing an answer to Stack Overflow!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Some of your past answers have not been well-received, and you're in danger of being blocked from answering.
Please pay close attention to the following guidance:
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53741270%2fchessboard-in-pure-js%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
9
Quick tip - press F12 and you'll be able to see any errors in your console.
– markmoxx
5 hours ago