Wow what a week! Last week’s challenge was a big hit. In case you missed it, here's a link to last week’s article here and the challenge on Coderby...
For further actions, you may consider blocking this person and/or reporting abuse
I would not use WebAnimation API :
As for requestAnimationFrame, isn't it overkill too ?
I would use CSS transition for this kind of stuff.
The tricky part is to avoid reseting things at each call, so you need to:
Nice work!!
Maintaining state is not really that big of a deal using the webanimation api. The element's position is always a property of the element. Even though it wasn't asked of the question to maintain the final position, this is what the method could look like if that were a concern:
Still simpler. I would think the question should have also asked for units to be passed in as an argument to make it far more versatile.
I agree with you though! I would prefer to keep all animation logic in CSS. When I first read the question, I was thinking exactly about that, but I was also wondering what Facebook's goal might be in asking this question. Are they hoping the candidate to be aware of the built-in JS methods? If so, it's a good question to see how well the candidate is keeping his/her knowledge current. Or, maybe it's an opportunity for the candidate who is current to teach the interviewer something new. Is Facebook hoping the candidate to be able to do better than the built-in or will they get worried if the candidate goes down a rabbit hole? Etc.
Working example on jsfiddle
Update 12/11/2019: I provide a more complete example below (thanks to Vincent's reasoning)
Well actually it's more about building re-usable function you can use in every context (as a NPM package for example) than a real "Facebook Challenge" (who knows what they are really expecting from it ?)
The problem I can see from yours (even if it's a really simple solution) :
In fact, this one with same bugs can be rewritten with CSS transition in less lines and better browser support :
So yeah, WebAnimation API is powerful and awesome, but should always be used depending of the context (but It was quite fun to see an actual usage, thanks for it !)
Interesting perspective. I like the points you bring up! Fwiw, I think the API will eventually address those concerns, which I thought would have been addressed by now in 12/2019, but still being worked on (so yes, my mistake, my apologies). Future options I think will preserve state a bit better.
Wow interesting I didn’t know about it!
With future options it’s gonna be a real game-changer!
Also, I think the
removeBackspacesmethod can further be reduced withreduce:Gives me the same result, I think, and helps guard against typical problems introduced by loops, such as "off-by-one" errors. (
array.reducehelps the iteration stay within the bounds of the array size)For the Facebook question, just "cheat"...just create a wrapper around the built-in JS web-animation API and be done with it in 2 seconds (
elm.animate(...)). The built-in isn't third party and technically not breaking directions...so...half cheating, I guess. No need to re-invent the wheel...Generally speaking, most interview questions are given despite well known methods / libraries being around that have already solved a problem! I have often been asked to implement lodash methods for example, even though in the real world, I'd just use lodash.
With a couple extra parameters.
Nice! Although this will move the element from a given position, rather than where it is on the page.
Thanks! Another fun challenge would be to add a timing function lerp, similar to cubic-bezier(x1, y1, x2, y2) to vary the speed over the course of the duration. It's a leap forward in complexity but quite doable.
This will only work if the element is absolute positioned.
Have you tried el.style.transform = "translate(x,y)"
My solution:
const animate = (element, miliseconds, distance) =>{
element.style.transition =
all ${miliseconds/1000}s ease;element.style.transform =
translateX(${distance}px);}
animate(document.querySelector('div'), 2000, 100)
This is what I got.
I think the challenge is asking if you know how to use requestanimationframe basically.
But that is crazy overkill for this task. The appropriate way to implement this in current year is to use CSS. If an animation can be done without issue in CSS you should never use JS to do it. That is actually kind of a good rule to extrapolate and follow for everything on the front-end: Use JS only for the things you can't do with other technologies.
Good point!
And, just a helpful link: Passing Parameters to CSS animation if anyone is interested... this would be the CSS way of doing it. JS events fire (like "scrolling", "mousemove") and the data from those events get passed to the css vars (but maybe someday we'd find it easier to pass an argument to a method in JS instead? easier to test maybe?). I think the webanimations API will surprise us all in [1-2 years?] and challenge us all as to what we do in JS vs CSS. I could be wrong. I'm still thinking about the pros and cons.
Nice solution!