Skip to main content
dublove
Legend
July 28, 2025
Answered

How to find even numbers in cp[i]?

  • July 28, 2025
  • 1 reply
  • 194 views

var cp = pp.marginPreferences.columnsPositions;

I now want to distinguish between odd and even numbers.
cp[1], cp[3], cp[5]
cp[2], cp[4], cp[6]
I previously used cp[2*i] to represent even numbers and cp[2*i-1] to represent odd numbers, but in practice, as the numbers grow larger, the logic becomes confusing.
For example, when i=11, 2*i becomes 22, which is no longer near 11.

 

I think the correct logic should be:
Odd number + 1 = Even number.

Correct answer m1b

@dublove you would use remainder operator (%). Something like this:

var doc = app.activeDocument;
var cp = doc.pages[0].marginPreferences.columnsPositions;
var even = [];
var odd = [];

for (var i = 0; i < cp.length; i++) {
    if (0 === i % 2)
        even.push(cp[i]);
    else 
        odd.push(cp[i]);
}

- Mark 

1 reply

m1b
Community Expert
m1bCommunity ExpertCorrect answer
Community Expert
July 28, 2025

@dublove you would use remainder operator (%). Something like this:

var doc = app.activeDocument;
var cp = doc.pages[0].marginPreferences.columnsPositions;
var even = [];
var odd = [];

for (var i = 0; i < cp.length; i++) {
    if (0 === i % 2)
        even.push(cp[i]);
    else 
        odd.push(cp[i]);
}

- Mark 

dublove
dubloveAuthor
Legend
July 28, 2025

Hi m1b,

It should be even.push(i);

I thought it could be done directly like this:
ev=even(i);

I understand now, thank you very much.

 

 

m1b
Community Expert
Community Expert
July 28, 2025

Yes `even` and `odd` in my example are Arrays, and push is a method to add something to the end of the array. So my example will put items 0,2,4,6,8 etc into even and items 1,3,5,7,9 etc into odd. Rememer that the first index of an array is 0, not 1.