From d2ea59a66c57a913c959f94f902322dbc6783d6d Mon Sep 17 00:00:00 2001 From: Liam Simmons Date: Wed, 15 May 2024 09:35:25 -0400 Subject: [PATCH] Add event listener to move dodger --- index.js | 33 ++++++++++++++++++++++++++++++++- 1 file changed, 32 insertions(+), 1 deletion(-) diff --git a/index.js b/index.js index 877a3aa08..0d3281c6d 100644 --- a/index.js +++ b/index.js @@ -1 +1,32 @@ -// Your code here +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(); + } +}); + + +