Once you figure out the modulus operator, it's a pretty cool operator! A ternary operator can get rid of that last if:
const imageData = [ 'image1.png', 'img2.png', 'img3.png' ]; let currentImage = 0; const handleImageChange = (direction) => { currentImage = (direction == 'forward') ? (currentImage + 1) % imageData.length : (currentImage - 1 + imageData.length) % imageData.length; }
(also changed currentImage to let (otherwise "Assignment to constant variable." error)
I wouldn't say a ternary is better than an if, especially for multi-line things like this.
if
Thanks. I fixed it. I do that all the time.
Are you sure you want to hide this comment? It will become hidden in your post, but will still be visible via the comment's permalink.
Hide child comments as well
Confirm
For further actions, you may consider blocking this person and/or reporting abuse
We're a place where coders share, stay up-to-date and grow their careers.
Once you figure out the modulus operator, it's a pretty cool operator! A ternary operator can get rid of that last if:
(also changed currentImage to let (otherwise "Assignment to constant variable." error)
I wouldn't say a ternary is better than an
if
, especially for multi-line things like this.Thanks. I fixed it. I do that all the time.