Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- var chessBoard = function () {
- var chessBoard = [];
- var alph = "abcdefgh".toUpperCase().split("");
- var evenAlph = 'aceg';
- //create properties in object chessBoard that are letters A-H (i.e, chessBoard.A, chessBoard.B or similarly chessBoard['A'] or chessBoard['B'] etc.
- for (var i = 0; i < alph.length; i++) {
- chessBoard[alph[i]] = [];
- }
- //loop through properties of chessBoard with for ... in. in each loop, "v in chessBoard" means like how i in for(var i = 0; ... i++) changes i each time starting with 0 and ending with whatever is specified (i.e. i < 5), v will become the properties of chessBoard each loop, starting with v = 'A' and ending with v = 'H'
- for (v in chessBoard) {
- // "if this property name is an even letter (evenAlph)"
- if (evenAlph.indexOf(v.toLowerCase()) !== -1) {
- //start with 0, end with 8 (make 8 objects: A1-A8, B1-B8, etc.
- for (var i = 0; i < 8; i++) {
- //i is even (if the remainder of i divided by two is; 0, i.e. 2 divides i evenly)
- if (i % 2 == 0) {
- //push with cell object, which is black
- chessBoard[v].push({
- black: true,
- white: false
- });
- } else {
- //push with cell object, which is white
- chessBoard[v].push({
- black: false,
- white: true
- });
- }
- }
- //otherwise (this property is not an evenAlph) we start with white, end black
- } else {
- for (var i = 0; i < 8; i++) {
- if (i % 2 == 0) {
- chessBoard[v].push({
- black: false,
- white: true
- });
- } else {
- chessBoard[v].push({
- black: true,
- white: false
- });
- }
- }
- }
- }
- return chessBoard;
- }();
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement