Why my code doesnt work? | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
+ 2

Why my code doesnt work?

I want it to move from left to right infinitely by pressing button. I have no idea whats wrong. https://code.sololearn.com/WvNyATS3B7TL/?ref=app

8th Aug 2019, 11:48 AM
a a
a a - avatar
11 Answers
+ 1
a way to do it: var goingRight; var arrow; var arrowPos; window.onload = function(){ goingRight = true; arrowPos = 170; arrow = document.getElementById("arrow"); loop(); } function loop(){ if(goingRight){ arrowPos += 1; if(arrowPos > 340){ goingRight = false; } }else{ arrowPos -= 1; if(arrowPos < 0){ goingRight = true; } } arrow.style.left = arrowPos + "px"; requestAnimationFrame(loop); }
8th Aug 2019, 11:59 AM
Anton Böhler
Anton Böhler - avatar
+ 2
Scoozak I just wanted to say that he forgot put style before left attribute
8th Aug 2019, 1:48 PM
Kelly (offline)
Kelly (offline) - avatar
+ 2
Scoozak i just wanna say i noticed one mistake i made and u didnt notice too: it goes to pos 340 but then stops! Ez to fix: return arrow_move_left() !! There were () missing!!
8th Aug 2019, 2:03 PM
a a
a a - avatar
+ 1
That code is great but i have questions: How often does it call function again? Whats wrong with my code? What would fix my code? Is it possible without requestAnimationFrame(cuz idk that yet and i dont want to use unknown codes...)? Thank you very much!
8th Aug 2019, 12:40 PM
a a
a a - avatar
8th Aug 2019, 12:58 PM
a a
a a - avatar
+ 1
It makes error
8th Aug 2019, 1:12 PM
a a
a a - avatar
0
requestAnimationFrame calls a function 60 a sec
8th Aug 2019, 12:43 PM
Anton Böhler
Anton Böhler - avatar
0
arrow = document.getElementById("arrow") var arrow_pos = 170; function arrow_move_right () { arrow_pos += 1; arrow.style.left = arrow_pos + "px"; if (arrow_pos == 340) { return arrow_move_left() } setTimeout(arrow_move_right, 10); }; function arrow_move_left () { arrow_pos -= 1; arrow.style.left = arrow_pos + "px"; if (arrow_pos == 0) { return arrow_move_right() } setTimeout(arrow_move_left, 10); }; //window.onload (arrow_move_right()) errors were: -a html-elements position is accessed using style -you forgot to call the function, you just returned the function name ...
8th Aug 2019, 12:46 PM
Anton Böhler
Anton Böhler - avatar
0
you have to press start ...
8th Aug 2019, 1:00 PM
Anton Böhler
Anton Böhler - avatar
0
really? for me it doesn't ... what does it say? 😅
8th Aug 2019, 1:16 PM
Anton Böhler
Anton Böhler - avatar
0
a a I think she was focused on error thats been corrupted
8th Aug 2019, 2:07 PM
Terkizas
Terkizas - avatar