C
chilipepas
Hello.
I receive from a database a variable number of results.
In my page, I can show a maximum of 36 results.
I want to "neatly" eliminate the exceeding results.
For example:
say, we have 40 results.
I must eliminate 4 results.
-->I must never eliminate the first and the last.
So, I have:
34-4=30;
30/4 returns 7,5 (rounded = 7);
the elements to eliminate will be:
7, 14, 21, 28
I must automate this procedure...
I tried the following solution:
// hardcoded number of results for testing purposes
totResults = 150;
// maximum number of items
maxItems = 36;
//
p = totResults/maxItems;
//
temp = [];
for (i=0; i<maxItems+1; i++) {
temp[Math.ceil(p*i)] = Math.ceil(p*i);
}
The creation of undefined elements is perfectly functional to my
purposes, so please don't worry about it.
The fact is that this algorhitm works in a certain number of cases but
sometimes it doesn't, dued to rounding issues.
Could someone help me? Thanks!
I receive from a database a variable number of results.
In my page, I can show a maximum of 36 results.
I want to "neatly" eliminate the exceeding results.
For example:
say, we have 40 results.
I must eliminate 4 results.
-->I must never eliminate the first and the last.
So, I have:
34-4=30;
30/4 returns 7,5 (rounded = 7);
the elements to eliminate will be:
7, 14, 21, 28
I must automate this procedure...
I tried the following solution:
// hardcoded number of results for testing purposes
totResults = 150;
// maximum number of items
maxItems = 36;
//
p = totResults/maxItems;
//
temp = [];
for (i=0; i<maxItems+1; i++) {
temp[Math.ceil(p*i)] = Math.ceil(p*i);
}
The creation of undefined elements is perfectly functional to my
purposes, so please don't worry about it.
The fact is that this algorhitm works in a certain number of cases but
sometimes it doesn't, dued to rounding issues.
Could someone help me? Thanks!