1

I have an array of strings

var characters = ["Jim", "Anna", "Nick", "Chris", "Peter"];

my initial position is Jim[0]. if i type right i want to go to the next one In this case to "Anna". I can do this by adding one to the current index.

The thing is getting more tricky when i type left from the initial position?

So how can i go from Jim[0] -> Peter[4]?

Something that works is

Jim[0] - 1 + characters.length which equals to  0 - 1 + 5 = 4

and this is Peter. But this seems incorrect because i cannot do that in the case that the current value is Anna[1] and i want to go to Jim[0]. As 1 - 1 + 5 = 5

  • If rotating the array meets your requirements, check this answer: http://stackoverflow.com/questions/1985260/javascript-array-rotate – Marcus Vinícius Monteiro Feb 14 '17 at 01:04
  • What language? In Java, int mod works with negative number so that you can do something like (assuming delta is number you want to move) `(((currentIndex + delta) % array.length) + array.length) %array.length`. I believe similar technique works in Javascript too – Adrian Shum Feb 14 '17 at 02:45

4 Answers4

0

You could do a conditional. Keep your index in a variable instead of hard-coding it. This will allow you to do something similar to the pseudocode following (Sorry, wasn't sure which language this was for):

new_index = index--
if (new_index < characters.length)
    index = characters.length - 1

You can then use that determined index to access the array. You could something similar to wrap around going forwards as well.

gallen
  • 1,000
  • 1
  • 15
  • 21
0

Something like this would work.

var characters = ["Jim", "Anna", "Nick", "Chris", "Peter"];
var index = 0; // Currently at Jim.
var direction = -1; // 1 for right, -1 for left.

// What's left of our current position?
characters[(index + characters.length + direction) % characters.length]; // Peter!
amix
  • 118
  • 1
  • 10
Jon G
  • 745
  • 5
  • 5
  • What's the point of using `direction` if you already have `index`? – pishpish Feb 13 '17 at 23:01
  • Hi Jon, this is the logic i was looking for. It looks like i was missunderstanding the % . Thanks i did learn something today :) –  Feb 14 '17 at 10:20
  • @changed The `index` was just to mark the current spot, and then `direction` was to flag if we were moving left or right. Really the solution was just the formula in the last line. – Jon G Feb 14 '17 at 14:32
  • @JonG That's cool, but your solution will break if `index + length < 0`. For example `(-7 + 5) % 5 = -2`. – pishpish Feb 14 '17 at 15:16
  • @changed `index` is a value between 0 and 4 in this case. The purpose of the formula is to take an existing index and move left or right without the new index being outside the bounds of the array. It is assumed you aren't already outside the bounds of the array. – Jon G Feb 14 '17 at 16:21
0

There seems to be no single formula to calculate the left and right index without using conditions.

If you don't intend to use if-else or just want a single line solution, you can use the ternary operator as follows:

new_index = (direction=="left")?(index==0)?characters.length-1:index-1:(index==characters.length-1)?0:index+1

Please note that the above code assumes there are only 2 possible directions.

0

This is what you're looking for

(i % characters.length) + characters.length) % characters.length

var characters = ["Jim", "Anna", "Nick", "Chris", "Peter"];
var index;
for( var i = -10; i < 10; i++ ){
  index = ((i % characters.length) + characters.length) % characters.length;
  console.log(i + " > " + index + " = " + characters[index]);
}
pishpish
  • 2,359
  • 11
  • 21