1

I'm currently trying to check if the current active index equals to a number in the following way:

8, 16, 24, 32, 40 and so on... If that's the case then the console should print out: Go down.

The described part above works fine, but I can't seem to get the opposite part working.


Let's say my currentIndex equals to 12 then the next possible go down index would be 16 (Works fine) and the next possible index to go up would be 9 since its only showing 8 items each time. Now I need to check if the currentIndex equals to 8 + 1 / 16 + 1 / 24 + 1 / 32 + 1 / 40 + 1 ... So the console would print out Go up.

How could I create my else if to achieve this?

var shownItems = 8;
var currentIndex = 12;

// Check if the currentIndex equals 8 / 16 / 24 / 32 / 40 ...
if(currentIndex / shownItems % 1 === 0) {
    console.log("Go down");
}
// Check if the currentIndex equals 8 - 7 / 16 - 7 / 24 - 7 / 32 - 7 / 40 - 7
else if() {
    console.log("Go up");
}
1
  • 4
    Hint: anyNumber % 1 === 0... Commented Apr 19, 2018 at 15:32

1 Answer 1

6

Just check for:

 (currentIndex + 1) % shownItems === 0
Sign up to request clarification or add additional context in comments.

2 Comments

Thanks, just had to change - 1 to + 1 and works fine!
@pr0b glad to help :)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.