Skip to content

Commit 702cd92

Browse files
committed
Ex5 fixed and completed
1 parent 10eb150 commit 702cd92

1 file changed

Lines changed: 23 additions & 33 deletions

File tree

‎Week1/js-exercises/ex5-catWalk.js‎

Lines changed: 23 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -11,39 +11,29 @@
1111
6. When the cat reaches the middle of the screen, replace the img with an image of a cat dancing(use this URL: https: //tenor.com/StFI.gif), keep it dancing for 5 seconds, and then replace the img with the original image and have it continue the walk.
1212
1313
*/
14-
function sleep(ms) {
15-
return new Promise(
16-
resolve => setTimeout(resolve, ms)
17-
);
18-
}
19-
2014
const catImg = document.querySelector('body img');
21-
catImg.style.left = '0';
22-
let point =0;
23-
function catWalk() {
24-
if (point > screen.width){
25-
point = 0 ;
26-
} else if (point == (screen.width / 2) ) {
27-
28-
catImg.src = 'tenor.gif';
29-
sleep(2000);
30-
catImg.src = 'http://www.anniemation.com/clip_art/images/cat-walk.gif';
31-
point++;
32-
33-
34-
} else {
35-
catImg.style.left = `${point}px`;
36-
point++;
37-
}
38-
}
15+
let point = -300;
16+
app();
17+
18+
function app() {
19+
20+
const walking = setInterval(catWalk, 5);
3921

40-
setInterval(catWalk, 5);
22+
function catWalk() {
23+
if (point > screen.width) {
24+
point = -300;
25+
} else if (point == (screen.width / 2) - 150) {
26+
clearInterval(walking);
27+
catImg.src = 'tenor.gif';
28+
setTimeout(() => {
29+
catImg.src = 'http://www.anniemation.com/clip_art/images/cat-walk.gif';
30+
point++;
31+
app();
32+
}, 5000);
33+
} else {
34+
catImg.style.left = `${point}px`;
35+
point++;
36+
};
37+
};
38+
};
4139

42-
//another ways
43-
// setInterval(function(){
44-
// if (point > screen.width){
45-
// point = -300 ;
46-
// }
47-
// catImg.style.left = point + 'px';
48-
// point += 10;
49-
// }, 50);

0 commit comments

Comments
 (0)