eliminate results

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • chilipepas@gmail.com

    eliminate results

    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!
  • Evertjan.

    #2
    Re: eliminate results

    wrote on 26 jul 2008 in comp.lang.javas cript:
    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)

    Comment

    • Thomas 'PointedEars' Lahn

      #3
      Re: eliminate results

      chilipepas@gmai l.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(star t, start + 36);


      PointedEars
      --
      var bugRiddenCrashP ronePieceOfJunk = (
      navigator.userA gent.indexOf('M SIE 5') != -1
      && navigator.userA gent.indexOf('M ac') != -1
      ) // Plone, register_functi on.js:16

      Comment

      • Piperita

        #4
        Re: eliminate results

          var trimmedResults = temp.slice(star t, 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.

        Comment

        • Evertjan.

          #5
          Re: eliminate results

          Piperita wrote on 26 jul 2008 in comp.lang.javas cript:
          >ÿ var trimmedResults = temp.slice(star t, 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(star t, 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)

          Comment

          • Thomas 'PointedEars' Lahn

            #6
            Re: eliminate results

            Piperita wrote:
            > var trimmedResults = temp.slice(star t, 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, <f806at$ail$1$8 300dec7@news.de mon.co.uk>

            Comment

            • Piperita

              #7
              Re: eliminate results

              var trimmedResults = temp.slice(star t, 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,und efined,undefine d,1,undefined,u ndefined,undefi ned,
              2,undefined,und efined,undefine d,3,undefined,u ndefined,undefi ned,4

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




              Comment

              • Piperita

                #8
                Re: eliminate results

                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$8 300d...@news.de mon.co.uk>

                Comment

                • Piperita

                  #9
                  Re: eliminate results

                  (fullArr)

                  temp (sorry)

                  Comment

                  • Evertjan.

                    #10
                    Re: eliminate results

                    Piperita wrote on 27 jul 2008 in comp.lang.javas cript:
                    >
                    >var trimmedResults = temp.slice(star t, 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,und efined,undefine d,1,undefined,u ndefined,undefi ned,
                    2,undefined,und efined,undefine d,3,undefined,u ndefined,undefi ned,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.lengt h); // 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.leng th); // 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)

                    Comment

                    • Piperita

                      #11
                      Re: eliminate results

                      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 VariableNumberO fRecords-FixedNumberOfIt ems
                      ("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,und efined,undefine d,1,undefined,u ndefined,undefi ned,
                      2,undefined,und efined,undefine d,3,undefined,u ndefined,undefi ned,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.

                      Comment

                      • Lasse Reichstein Nielsen

                        #12
                        Re: eliminate results

                        Piperita <chilipepas@gma il.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((tem p.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/rasterTriangleD OM.html>
                        'Faith without judgement merely degrades the spirit divine.'

                        Comment

                        • Piperita

                          #13
                          Re: eliminate results


                          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.



                          Comment

                          • Thomas 'PointedEars' Lahn

                            #14
                            Re: eliminate results

                            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 bugRiddenCrashP ronePieceOfJunk = (
                            navigator.userA gent.indexOf('M SIE 5') != -1
                            && navigator.userA gent.indexOf('M ac') != -1
                            ) // Plone, register_functi on.js:16

                            Comment

                            • Piperita

                              #15
                              Re: eliminate results

                              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?

                              Comment

                              Working...