From ed404c8f7b23b52138007515e5637821bfa35f66 Mon Sep 17 00:00:00 2001 From: Jack Pantalena Date: Sun, 4 Dec 2016 15:39:53 -0700 Subject: [PATCH] exercise complete --- script.js | 54 +++++++++++++++++++++++++++++++++++++++++++++++++++++- 1 file changed, 53 insertions(+), 1 deletion(-) diff --git a/script.js b/script.js index 9233771b..0cbd8dbb 100644 --- a/script.js +++ b/script.js @@ -1 +1,53 @@ -// Your JS goes here \ No newline at end of file +function createRows(number) { + var body = document.getElementsByTagName('body')[0]; + for (var i = 0; i < number; i++) { + var row = document.createElement('section'); + row.className = "row"; + row.setAttribute("style", "display:inline-block;width:60px;"); + body.appendChild(row); + } +} + +function addTiles() { + var row_array = document.querySelectorAll('.row'); + row_array.forEach(function(element) { + for (var i = 0; i < 9; i++) { + var tile = document.createElement('div'); + tile.className = "tile"; + tile.innerHTML = " "; + element.appendChild(tile); + } + }); +} + +function styleTiles() { + var columns = document.querySelectorAll('section'); + for (var i = 0; i < columns.length; i++) { + var divs = columns[i].childNodes; + if (i%2 === 0) { + divs.forEach( function(element, index) { + if (index%2 === 0) { + element.setAttribute("style", "background-color:red; width:100%; height:60px;"); + } else { + element.setAttribute("style", "background-color:black; width:100%; height:60px"); + } + }); + } else { + divs.forEach( function(element, index) { + if (index%2 !== 0) { + element.setAttribute("style", "background-color:red; width:100%; height:60px;"); + } else { + element.setAttribute("style", "background-color:black; width:100%; height:60px"); + } + }); + } + } +} + +function checkerboard() { + createRows(9); + addTiles(); + styleTiles(); +} + +checkerboard();