Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

202
Vistas
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 Respuestas
Responde la pregunta

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 Denunciar
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda