javascript - How to increment or dicrement from current index within the total -


i looping number between 0 total have. thing is, need create currentindex , should not more total same way need dicreement value currentindex not less 0.

i tried nothing come handy. here try:

var total = 6; var currentindex = 3; var num = 0; function add(amount) {    return num = (num + total - currentindex + amount) % total + 1  }   $('a').click(function(e){     var num = e.target.classname == 'prev' ? -1 : 1;     var result = add(num)+currentindex;     console.log(result); }); 

jsfiddle

you need think simpler, on right track:

var total = 6;  var currentindex = 3;  var num = 0;  function add(amount) {     currentindex=((currentindex+amount%total)+total)%total;  }      $('a').click(function(e){      var num = e.target.classname == 'prev' ? -1 : 1;      add(num);      console.log(currentindex);  });
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>  <a href="#" class="prev">prev</a>  <a href="#" class="next">next</a>

this loop between 0 , 5 inclusive.


Comments

Popular posts from this blog

c# - Validate object ID from GET to POST -

node.js - Custom Model Validator SailsJS -

php - Find a regex to take part of Email -