From d43d48362b719ecef3360f32f40d708172a31e0e Mon Sep 17 00:00:00 2001 From: jim-clark Date: Fri, 26 Jul 2019 14:34:20 -0700 Subject: [PATCH] Update state and call render in handleClick --- js/main.js | 18 ++++++++++++++++-- 1 file changed, 16 insertions(+), 2 deletions(-) diff --git a/js/main.js b/js/main.js index 0b45cd3..84aec9e 100644 --- a/js/main.js +++ b/js/main.js @@ -44,8 +44,22 @@ function render() { } function handleClick(evt) { + // get index of column's marker clicked let idx = parseInt(evt.target.id.replace('col', '')); + // make sure the MARKER was clicked if (isNaN(idx) || winner) return; - - + // obtain the actual column array in board array + let colArr = board[idx]; + // get the index of the first 0 in the col array + let rowIdx = colArr.indexOf(0); + // if the col is full, there are no zeroes, therefore + // indexOf returns -1. + // Do nothing if no zeroes available (col full) + if (rowIdx === -1) return; + // update the col array (within the board) with + // the player whose turn it is + colArr[rowIdx] = turn; + // flip turns from 1 to -1; -1 to 1 + turn *= -1; + render(); } \ No newline at end of file