eliminate results

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!
 
E

Evertjan.

wrote on 26 jul 2008 in comp.lang.javascript:
Hello.

I receive from a database a variable number of results.


Seems school assignment to me.

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!
 
T

Thomas 'PointedEars' Lahn

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.
[...]
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!

var trimmedResults = temp.slice(start, start + 36);


PointedEars
 
P

Piperita

  var trimmedResults = temp.slice(start, start + 36);



Hi, and thanks for your kind reply.
The fact is that temp will contain more than 36 results:
many results will be undefined and I need them as they are.
By trimming that array the way you do I will not get the expected
result.
Anyway, thanks very much.
 
E

Evertjan.

Piperita wrote on 26 jul 2008 in comp.lang.javascript:
Hi, and thanks for your kind reply.
The fact is that temp will contain more than 36 results:
many results will be undefined and I need them as they are.
By trimming that array the way you do I will not get the expected
result.


How could you expect not to get what you expect to get?

=======================

var trimmedResults = temp.slice(start, start + 36);

If temp is not a string, but an array,
this is not trimming,
but selecting part of an array as a new array.

As expected, methinks.
 
T

Thomas 'PointedEars' Lahn

Piperita said:
var trimmedResults = temp.slice(start, start + 36);

[...]
The fact is that temp will contain more than 36 results:
many results will be undefined and I need them as they are.
By trimming that array the way you do I will not get the expected
result.

Shouldn't you test suggestions you asked for before you dismiss them out of
hand?

The returned object reference will refer to an Array object that has a
`length' property that corresponds to the number of elements in the array,
regardless of their value. (Even if not, there would be no problem at all
testing an item accessed by its index for its type and iterate up to 36
nonetheless.)
Anyway, thanks very much.

What for, after all?


PointedEars
 
P

Piperita

var trimmedResults = temp.slice(start, start + 36);

If temp is not a string, but an array,
this is not trimming,
but selecting part of an array as a new array.

As expected, methinks.


Hi and thanks for taking the time to answer.
I don't understand why we're creating new arrays.

I'm in the need of one array (fullArr) containing undefined values
between numeric ones, something like this one:
0,undefined,undefined,undefined,1,undefined,undefined,undefined,
2,undefined,undefined,undefined,3,undefined,undefined,undefined,4

This would be the complete fullArr if I should show 5 results with 12
exceeding results.
 
P

Piperita

Shouldn't you test suggestions you asked for before you dismiss them out of
hand?



You're right


The returned object reference will refer to an Array object that has a
`length' property that corresponds to the number of elements in the array,
regardless of their value.  (Even if not, there would be no problem at all
testing an item accessed by its index for its type and iterate up to 36
nonetheless.)



uhm, maybe my explanation was not very clear.
The number of elements in temp is variable: there can be 140, 145, 134
elements.
The maximun number of items *to show* is 36: I should equally
distribute exceedings between elements.

Now, if "start" is 0 (as it should be) and I cut off an array of 36
elements it seems I do not solve my issue.
But maybe I'm not perfectly getting your suggestion. :-(
 
E

Evertjan.

Piperita wrote on 27 jul 2008 in comp.lang.javascript:
Hi and thanks for taking the time to answer.
I don't understand why we're creating new arrays.

seems to me you ask for a part of an array, why not make a new array with
that part, usinf slice() in stead of plowing through the first array.
I'm in the need of one array (fullArr) containing undefined values
between numeric ones, something like this one:
0,undefined,undefined,undefined,1,undefined,undefined,undefined,
2,undefined,undefined,undefined,3,undefined,undefined,undefined,4

Sorry, you're not very clear, you want this?

=====================

// defining:
var arr = [];
arr[0] = 0;
arr[4] = 1;
arr[16] = 9999;
arr[8] = 2;
arr[12] = 3;
arr[17] = 4;
arr[33] = 99;


// test:
alert(arr.length); // 34


// slicing and eliminating:
var arr2 = arr.slice(0,18); // slicing
arr2[16] = undefined; // elimination
// This slicing and eliminating
// can be programmed according to your rules.
// If you do not need the first arr[] anymore,
// you could reuse the array name arr,
// instead of creating arr2[].


// test:
alert(arr2.length); // 18
alert(arr2[3]); // undefined
alert(arr2[16]); // undefined
alert(arr2[17]); // 4
=====================
This would be the complete fullArr if I should show 5 results with 12
exceeding results.

Please refrase:
An array "is" not "shows".
What are "exeeding results"?
 
P

Piperita

Please refrase:
An array "is" not "shows".
What are "exeeding results"?



Hi, give me the time to read and respectfully study your solution.

I try to explain better.

1. I receive a variable number of records from a database;
2. I can print on screen only a defined and fixed number of items;
3. How many records will I "eliminate" (not print) and which ones?
3a. I will delete VariableNumberOfRecords-FixedNumberOfItems
("exceeding");
3b. Which ones? Not at the end, not at the beginning, but I will
equally distribute the exceeding quota of records;

This array is very functional for my app (moreover, I reuse this model
later in the program):

0,undefined,undefined,undefined,1,undefined,undefined,undefined,
2,undefined,undefined,undefined,3,undefined,undefined,undefined,4
(0,1,2,3,4 correpond to selected records I will print on screen)

This one would be perfect for a situation with:

FIXED NUMBER: 5;
VARIABLE RECORDS: 17;
(so exceeding records to transform into undefined values: 12)

Is this clear enough? Sorry for my English and thanks for your time.
 
L

Lasse Reichstein Nielsen

Piperita said:
The number of elements in temp is variable: there can be 140, 145, 134
elements.
The maximun number of items *to show* is 36: I should equally
distribute exceedings between elements.

function extract36(temp) {
if (temp.length <= 36) { return temp; }
var toShow = [];
for(var i = 0; i <= 35; i++) {
toShow = temp[Math.floor((temp.length - 1) * i / 35)];
}
return toShow;
}

This should extract 36 values, approximately equally spread througout
temp, including the first and last value.

/L
 
P

Piperita

Hi sorry for the late answer but I'm coming from work now and I've a
little bit of time.
I found this solution myself but this solution means two for loops. It
is absolutely ok but I was trying to do the same thing in one step.
Thanks you very much, Lasse.
 
T

Thomas 'PointedEars' Lahn

Piperita said:
I found this solution myself but this solution means two for loops.

I only see one `for' loop here. What would be the second one?

Please quote the minimum of what you are replying to.


PointedEars
 
P

Piperita

I only see one `for' loop here.  What would be the second one?


I think the first is needed to generate temp, the second is the one
inside the function body.
Am I wrong?
 
T

Thomas 'PointedEars' Lahn

Piperita said:
I think the first is needed to generate temp, the second is the one
inside the function body.
Am I wrong?

How you fill `temp' is rather irrelevant to the solution. Suppose it
contains the results of the database query as you suggested, then you would
initialize `temp' with those results through a server-side script that
generates the constructor call or initializer literal.

However, if you are to increase efficiency, you would have the server-side
script generate the initializer appropriately using the presented algorithm
and no client-side script to filter for the maximum number of displayed
items. But then it might not even be a JS solution that is needed.

Please do not remove the attribution line.


PointedEars
 
P

Piperita

However, if you are to increase efficiency, you would have the server-side
script generate the initializer appropriately using the presented algorithm
and no client-side script to filter for the maximum number of displayed
items.  But then it might not even be a JS solution that is needed.



I can't change the server-side: it serves data for many different
clients (JS, Flash, other) and by changing the server-side script, you
must update all the clients. This is not possible.
 
T

Thomas 'PointedEars' Lahn

Piperita said:
I can't change the server-side: it serves data for many different
clients (JS, Flash, other) and by changing the server-side script, you
must update all the clients. This is not possible.

Still, I do not see the second loop you are talking about. Either the
server-side script generates the client-side array, then you have one `for'
loop to iterate over it; or you retrieve the records in chunks from the
server, then you append only those to your target array that fit the
condition for almost equal distribution. The latter would require you to
know how many records there are in total, though.


PointedEars
 
P

Piperita

Still, I do not see the second loop you are talking about.



I think the function presumes you have built the array temp (passed as
an argument).
I built that array inside another for loop.

I get from the server all the records, I must filter them, considered
the fact that:
1. I can show (print) a certain number of items;
2. The "unprinted" ones must represent themselves as "undefined"
elements inside an array;
3. The undefined element must appear "equally" between printed items;
There's no other specification, except for the fact that the first and
the last elements must never be undefined.
That array (temp) is very important because the rest of the program
uses it in many ways.
The fact is that the bug in the code I first posted has been
discovered late.
Thanks for your precious and patient help.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,582
Members
45,059
Latest member
cryptoseoagencies

Latest Threads

Top