pr0b
pr0b

Reputation: 377

Javascript check if an index equals a number

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");
}

Upvotes: 1

Views: 445

Answers (1)

Jonas Wilms
Jonas Wilms

Reputation: 138267

Just check for:

 (currentIndex + 1) % shownItems === 0

Upvotes: 6

Related Questions