By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
438,481 Members | 1,713 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 438,481 IT Pros & Developers. It's quick & easy.

eliminate results

P: n/a
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!
Jul 26 '08 #1
Share this Question
Share on Google+
18 Replies


P: n/a
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!


--
Evertjan.
The Netherlands.
(Please change the x'es to dots in my emailaddress)
Jul 26 '08 #2

P: n/a
ch********@gmail.com wrote:
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
--
var bugRiddenCrashPronePieceOfJunk = (
navigator.userAgent.indexOf('MSIE 5') != -1
&& navigator.userAgent.indexOf('Mac') != -1
) // Plone, register_function.js:16
Jul 26 '08 #3

P: n/a
* 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.
Jul 26 '08 #4

P: n/a
Piperita wrote on 26 jul 2008 in comp.lang.javascript:
> 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.

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.

--
Evertjan.
The Netherlands.
(Please change the x'es to dots in my emailaddress)
Jul 26 '08 #5

P: n/a
Piperita wrote:
> 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
--
Prototype.js was written by people who don't know javascript for people
who don't know javascript. People who don't know javascript are not
the best source of advice on designing systems that use javascript.
-- Richard Cornford, cljs, <f8*******************@news.demon.co.uk>
Jul 26 '08 #6

P: n/a
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,undefi ned,undefined,
2,undefined,undefined,undefined,3,undefined,undefi ned,undefined,4

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


Jul 27 '08 #7

P: n/a
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. :-(


Anyway, thanks very much.

What for, after all?

PointedEars
--
Prototype.js was written by people who don't know javascript for people
who don't know javascript. People who don't know javascript are not
the best source of advice on designing systems that use javascript.
* -- Richard Cornford, cljs, <f806at$ail$1$8300d...@news.demon.co.uk>
Jul 27 '08 #8

P: n/a
(fullArr)

temp (sorry)
Jul 27 '08 #9

P: n/a
Piperita wrote on 27 jul 2008 in comp.lang.javascript:
>
>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.
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,undefi ned,undefined,
2,undefined,undefined,undefined,3,undefined,undefi ned,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"?
--
Evertjan.
The Netherlands.
(Please change the x'es to dots in my emailaddress)
Jul 27 '08 #10

P: n/a
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"?


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,undefi ned,undefined,
2,undefined,undefined,undefined,3,undefined,undefi ned,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.
Jul 27 '08 #11

P: n/a
Piperita <ch********@gmail.comwrites:
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[i] = 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
--
Lasse Reichstein Nielsen
DHTML Death Colors: <URL:http://www.infimum.dk/HTML/rasterTriangleDOM.html>
'Faith without judgement merely degrades the spirit divine.'
Jul 27 '08 #12

P: n/a

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.

Jul 28 '08 #13

P: n/a
Piperita wrote:
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
--
var bugRiddenCrashPronePieceOfJunk = (
navigator.userAgent.indexOf('MSIE 5') != -1
&& navigator.userAgent.indexOf('Mac') != -1
) // Plone, register_function.js:16
Jul 28 '08 #14

P: n/a
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?
Jul 28 '08 #15

P: n/a
Piperita wrote:
>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?
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
Jul 28 '08 #16

P: n/a
On 29 Lug, 01:26, Thomas 'PointedEars' Lahn <PointedE...@web.de>
wrote:
>
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.
Jul 30 '08 #17

P: n/a
Piperita wrote:
Thomas 'PointedEars' Lahn wrote:
>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.
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
--
Prototype.js was written by people who don't know javascript for people
who don't know javascript. People who don't know javascript are not
the best source of advice on designing systems that use javascript.
-- Richard Cornford, cljs, <f8*******************@news.demon.co.uk>
Jul 30 '08 #18

P: n/a
Thomas 'PointedEars' Lahn wrote:
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.
Jul 30 '08 #19

This discussion thread is closed

Replies have been disabled for this discussion.