From e160a3c955c96dba056b5430db8bff5f33ae11c7 Mon Sep 17 00:00:00 2001 From: Cayla Horsey Date: Tue, 14 May 2024 09:54:58 -0400 Subject: [PATCH 1/2] Add fucntion to move dodger right --- index.js | 32 ++++++++++++++++++++++++++++++++ 1 file changed, 32 insertions(+) diff --git a/index.js b/index.js index 877a3aa08..bc2b23ed8 100644 --- a/index.js +++ b/index.js @@ -1 +1,33 @@ // Your code here +const dodger = document.getElementById("dodger"); + +function moveDodgerLeft() { + const leftNumbers = dodger.style.left.replace("px", ""); + const left = parseInt(leftNumbers, 10); + + if (left > 0) { + dodger.style.left = `${left - 1}px`; + } +} + +document.addEventListener("keydown", function (e) { + if (e.key === "ArrowLeft") { + moveDodgerLeft(); + } +}); + +function moveDodgerRight() { + const leftNumbers = dodger.style.left.replace("px", ""); + const left = parseInt(leftNumbers, 10); + + if (left < 360) { + dodger.style.left = `${left + 1}px`; + } +} + +document.addEventListener("keydown", function (e) { + if (e.key === "ArrowRight") { + moveDodgerRight(); + } +}); + From f36bfbba9dce5a485878bec46f6349744e374de6 Mon Sep 17 00:00:00 2001 From: Cayla Horsey Date: Wed, 15 May 2024 10:38:23 -0400 Subject: [PATCH 2/2] Refactor event listeners --- index.js | 9 ++------- 1 file changed, 2 insertions(+), 7 deletions(-) diff --git a/index.js b/index.js index bc2b23ed8..15950016d 100644 --- a/index.js +++ b/index.js @@ -13,6 +13,8 @@ function moveDodgerLeft() { document.addEventListener("keydown", function (e) { if (e.key === "ArrowLeft") { moveDodgerLeft(); + } else if (e.key === "ArrowRight") { + moveDodgerRight(); } }); @@ -24,10 +26,3 @@ function moveDodgerRight() { dodger.style.left = `${left + 1}px`; } } - -document.addEventListener("keydown", function (e) { - if (e.key === "ArrowRight") { - moveDodgerRight(); - } -}); -