From 636622370eb72265572b9f2134cdc6c82d5eaf40 Mon Sep 17 00:00:00 2001 From: hkarimx Date: Sun, 5 Oct 2025 23:19:45 +0200 Subject: [PATCH] Add week1 prep-exercises --- .idea/.gitignore | 8 +++++ .../1-catwalk-promises/index.js | 36 +++++++++++-------- 2 files changed, 30 insertions(+), 14 deletions(-) create mode 100644 .idea/.gitignore diff --git a/.idea/.gitignore b/.idea/.gitignore new file mode 100644 index 00000000..13566b81 --- /dev/null +++ b/.idea/.gitignore @@ -0,0 +1,8 @@ +# Default ignored files +/shelf/ +/workspace.xml +# Editor-based HTTP Client requests +/httpRequests/ +# Datasource local storage ignored files +/dataSources/ +/dataSources.local.xml diff --git a/Week1/prep-exercises/1-catwalk-promises/index.js b/Week1/prep-exercises/1-catwalk-promises/index.js index dc07a9fe..04dcdc73 100644 --- a/Week1/prep-exercises/1-catwalk-promises/index.js +++ b/Week1/prep-exercises/1-catwalk-promises/index.js @@ -5,36 +5,44 @@ const STEP_INTERVAL_MS = 50; const DANCE_TIME_MS = 5000; const DANCING_CAT_URL = 'https://media1.tenor.com/images/2de63e950fb254920054f9bd081e8157/tenor.gif'; +const WALKING_CAT_URL = 'http://www.anniemation.com/clip_art/images/cat-walk.gif'; function walk(img, startPos, stopPos) { return new Promise((resolve) => { - // Resolve this promise when the cat (`img`) has walked from `startPos` to - // `stopPos`. - // Make good use of the `STEP_INTERVAL_PX` and `STEP_INTERVAL_MS` - // constants. + let pos = startPos; + const timer = setInterval(() => { + pos += STEP_SIZE_PX; + img.style.left = `${pos}px`; + if (pos >= stopPos) { + clearInterval(timer); + resolve(); + } + }, STEP_INTERVAL_MS); }); } function dance(img) { return new Promise((resolve) => { - // Switch the `.src` of the `img` from the walking cat to the dancing cat - // and, after a timeout, reset the `img` back to the walking cat. Then - // resolve the promise. - // Make good use of the `DANCING_CAT_URL` and `DANCE_TIME_MS` constants. + const originalSrc = img.src; + img.src = DANCING_CAT_URL; + setTimeout(() => { + img.src = originalSrc; + resolve(); + }, DANCE_TIME_MS); }); } -function catWalk() { +async function catWalk() { const img = document.querySelector('img'); const startPos = -img.width; const centerPos = (window.innerWidth - img.width) / 2; const stopPos = window.innerWidth; - // Use the `walk()` and `dance()` functions to let the cat do the following: - // 1. Walk from `startPos` to `centerPos`. - // 2. Then dance for 5 secs. - // 3. Then walk from `centerPos` to `stopPos`. - // 4. Repeat the first three steps indefinitely. + while (true) { + await walk(img, startPos, centerPos); + await dance(img); + await walk(img, centerPos, stopPos); + } } window.addEventListener('load', catWalk); \ No newline at end of file