Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

211
Views
How to loop an addition and substraction on a base of 3 in query?

I have a simpple Slider with 3 slides. Now everything works, but if I press more than 3 times in one direction I get "false" numbers. Mathemathically they are right, but Im aiming for this scenario:

If i click 4 times the button for previous slide, the counter of my numbers should be again 3, not -1. Any way to archive this?

  $( document ).ready(function() {
    
  var current =  $('.slider_number').data("hasso");
  var total = $('.slider_number').data("total")

  $('.slider_next').click(function(){
  ++current;
  console.log(current)
  $(".slider_number").text(current)
});
  $('.slider_prev').click(function(){
  --current;
  console.log(current)
}                                     
);
 });
about 4 years ago · Juan Pablo Isaza
1 answers
Answer question

0

Use the circular index formula (i % n + n) % n where n is the length of the array (4 in your case).

use:

current = (current+1 % total + total) % total;

to move forward;

and

current = (current-1 % total + total) % total;

to move backwards.

current will always be a valid index.

about 4 years ago · Juan Pablo Isaza Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!