From dde5da317488fd777d78c262547d73a69aac66b0 Mon Sep 17 00:00:00 2001 From: lolabird Date: Tue, 7 Nov 2023 20:28:51 -0700 Subject: [PATCH] Move event listener outside of function and start game within event listener --- script.js | 29 ++++++++++++++--------------- 1 file changed, 14 insertions(+), 15 deletions(-) diff --git a/script.js b/script.js index c4a63f1..a8515ee 100644 --- a/script.js +++ b/script.js @@ -41,20 +41,8 @@ function playRound(playerSelection, computerSelection){ } } -function game(ps) { - let p = 0; - let c = 0; - - const buttons = document.querySelectorAll('button'); - - buttons.forEach((button) => { - button.addEventListener('click', () => { - console.log(button.id); - }); - }); - - while (p < 5 && c < 5) { - const ps = getPlayerChoice(); +function game() { + if (p < 5 && c < 5) { const cs = getComputerChoice(); play = playRound(ps, cs); @@ -91,4 +79,15 @@ function game(ps) { } } -console.log(game()); \ No newline at end of file + +let p = 0; +let c = 0; +let ps; // Define the external variable for user's choice +const buttons = document.querySelectorAll('button'); + +buttons.forEach((button) => { + button.addEventListener('click', () => { + ps = button.id; // Assign the button's id to the external variable + game(); // Start the game when the user makes a choice + }); +}); \ No newline at end of file