Copy link to clipboard
Copied
Hi All,
I am looking for the solution to fix Index pages.
I have an array and need pages sorted as per below example:
var myArray = [5, 6, 7, 43, 45, 65, 66, 77, 98, 99, 100, 123, 155, 166]
output = [5ff, 43, 45, 65f, 77, 98ff, 123, 155, 166]
so logic is if there is page range continue more than 2 thfn "ff" will add and if its just more than one time then "f". I am unable to logic this loop so any help will be appreciated.
Thanks,
Shonky
1 Correct answer
Something like the following should work
function convertArray(arr) {
var i = 0;
while (i < arr.length) {
var start = i;
while (i + 1 < arr.length && arr[i] + 1 === arr[i + 1]) {
i++;
}
var end = i;
if (end - start > 1) {
arr[start] = arr[start] + 'ff';
// Remove all elements in the middle of the sequence
arr.splice(start + 1, end - start);
i = start + 1; // Move to the next element after
...
Copy link to clipboard
Copied
As your array is already sorted - you just need to check if next element is +1 of the current, if it is, then store index of this element, then keep checking - if only +1 - then add "f" to the saved index - if there is more - then add "ff".
Copy link to clipboard
Copied
Thanks Robert!
Copy link to clipboard
Copied
Something like the following should work
function convertArray(arr) {
var i = 0;
while (i < arr.length) {
var start = i;
while (i + 1 < arr.length && arr[i] + 1 === arr[i + 1]) {
i++;
}
var end = i;
if (end - start > 1) {
arr[start] = arr[start] + 'ff';
// Remove all elements in the middle of the sequence
arr.splice(start + 1, end - start);
i = start + 1; // Move to the next element after the modified one
} else if (end - start === 1) {
arr[start] = arr[start] + 'f';
// Remove the second element in the sequence
arr.splice(start + 1, 1);
i = start + 1; // Move to the next element after the modified one
} else {
i++;
}
}
return arr;
}
var arr = [5, 6, 7, 43, 45, 65, 66, 77, 98, 99, 100, 123, 155, 166];
convertArray(arr);
$.writeln(arr);
-Manan
Copy link to clipboard
Copied
Thanks Manan!!
Copy link to clipboard
Copied
Hi all,
For those who use IdExtenso, the module $$.PageRange supports the `f`, `ff` suffixes (reduxPair and reduxMore parameters). Full code here with all options explained:
https://github.com/indiscripts/IdExtenso/blob/master/etc/$$.PageRange.jsxlib#L519
(Note: this algorithm is used in IndexMatic³.)
Best,
Marc

