Page 1 of 2 12 LastLast
Results 1 to 10 of 12

Thread: how to sort this code?

  1. #1
    Join Date
    Aug 2008
    Location
    karanganyar, solo, indonesia
    Posts
    161
    Thanks
    4
    Thanked 0 Times in 0 Posts

    Default how to sort this code?

    hei, i can create a code on js, but i'm not able to make it shorter.
    can anyone here help me to sort this code?

    function checkingMyNewComm(opt){
    var comm_profile = myNewComm('Profile','check',false);
    var comm_blog = myNewComm('Blog','check',false);
    var comm_photo = myNewComm('Photo','check',false);
    var comm_music = myNewComm('Music','check',false);
    var comm_video = myNewComm('Video','check',false);
    var comm_classified = myNewComm('Classified','check',false);
    var comm_guestbook = myNewComm('Guestbook','check',false);
    return comm_profile+comm_blog+comm_photo+comm_music+comm_video+comm_classified+comm_guestbook;
    }

    thanks....
    ///////////////////////////////////////////////////
    ///// http://www.mediatutorial.web.id
    ///////////////////////////////////////////////////

  2. #2
    Join Date
    Aug 2008
    Location
    karanganyar, solo, indonesia
    Posts
    161
    Thanks
    4
    Thanked 0 Times in 0 Posts

    Default

    may be we can sort it by using array(Profile,Blog,Photo,Video,Music,Classified,Guestbook) , but i don't know how

    hum?
    ///////////////////////////////////////////////////
    ///// http://www.mediatutorial.web.id
    ///////////////////////////////////////////////////

  3. #3
    Join Date
    Sep 2005
    Location
    India
    Posts
    1,627
    Thanks
    6
    Thanked 107 Times in 107 Posts

    Default

    You can do the same thing mentioned in your code through the following code also.
    Code:
    function checkingMyNewComm(opt){
    	var arr = [];
    	var firstparam = ['Profile','Blog','Photo','Music','Video','Classified','Guestbook'];
    	for(var i = 0; i < firstparam.length; ++i){
    		arr.push(myNewComm(firstparam[i],'check',false));
    	}
    	return arr.join(' ');
    }
    In the refined version I've removed the multiple occurences of 'myNewComm' using a looping construct. As well as instead of multiple local variables I've used an array to store the result. Finally I've joined the resultant array content and delimited by space(' ').

    Hope this helps

  4. #4
    Join Date
    Aug 2008
    Location
    karanganyar, solo, indonesia
    Posts
    161
    Thanks
    4
    Thanked 0 Times in 0 Posts

    Default

    hum... let me think
    ///////////////////////////////////////////////////
    ///// http://www.mediatutorial.web.id
    ///////////////////////////////////////////////////

  5. #5
    Join Date
    Aug 2008
    Location
    karanganyar, solo, indonesia
    Posts
    161
    Thanks
    4
    Thanked 0 Times in 0 Posts

    Default

    does the value of "return comm_profile+comm_blog+comm_photo+comm_music+comm_video+comm_classified+comm_guestbook;" same as "return arr.join(' ');" ?
    ///////////////////////////////////////////////////
    ///// http://www.mediatutorial.web.id
    ///////////////////////////////////////////////////

  6. #6
    Join Date
    Sep 2005
    Location
    India
    Posts
    1,627
    Thanks
    6
    Thanked 107 Times in 107 Posts

    Default

    As I've mentioned in my post earlier, I have used arrays instead of scalar variables in my version. The step you've mentioned is similar to the one I've used the only difference is I have mentioned to insert a space between two items. Do have a look at here to learn more about the join method associated with the Array object.

  7. #7
    Join Date
    Aug 2008
    Location
    karanganyar, solo, indonesia
    Posts
    161
    Thanks
    4
    Thanked 0 Times in 0 Posts

    Default

    hum,
    return comm_profile+comm_blog+comm_photo+comm_music+comm_video+comm_classified+comm_guestbook
    will return 12 (2+2+1+3+4 and so on ) so the result is 12,

    i think your code will result 22134

    but i have not tried yet
    ///////////////////////////////////////////////////
    ///// http://www.mediatutorial.web.id
    ///////////////////////////////////////////////////

  8. #8
    Join Date
    Aug 2008
    Location
    karanganyar, solo, indonesia
    Posts
    161
    Thanks
    4
    Thanked 0 Times in 0 Posts

    Default

    Hey, by the way thanks for explanation. You're really help
    ///////////////////////////////////////////////////
    ///// http://www.mediatutorial.web.id
    ///////////////////////////////////////////////////

  9. #9
    Join Date
    Mar 2005
    Location
    SE PA USA
    Posts
    30,495
    Thanks
    82
    Thanked 3,449 Times in 3,410 Posts
    Blog Entries
    12

    Default

    Code:
    var checkingMyNewComm = function(){
     var m = myNewComm, c = 'check', f = false;
     return m('Profile', c, f) + m('Blog', c, f) + m('Photo', c, f) + m('Music', c, f) +
     m('Video', c, f) + m('Classified', c, f) + m('Guestbook', c, f);
    };
    or:

    Code:
    var checkingMyNewComm = function(){
     var a = ['Profile', 'Blog', 'Photo', 'Music', 'Video', 'Classified', 'Guestbook'];
     for (var t = 0, i = a.length - 1; i > -1; --i)
      t += myNewComm(a[i], 'check', false);
     return t;
    };
    Last edited by jscheuer1; 01-08-2009 at 08:26 AM. Reason: add another option
    - John
    ________________________

    Show Additional Thanks: International Rescue Committee - Donate or: The Ocean Conservancy - Donate or: PayPal - Donate

  10. #10
    Join Date
    Sep 2005
    Location
    India
    Posts
    1,627
    Thanks
    6
    Thanked 107 Times in 107 Posts

    Default

    John what do you think about the string concatenation through the + operator compared to the join method? What is the performance overhead? I mean not with this input but with a larger number of inputs..

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •