View Full Version : how to sort this code?

01-08-2009, 06:43 AM
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;


01-08-2009, 06:46 AM
may be we can sort it by using array(Profile,Blog,Photo,Video,Music,Classified,Guestbook) , but i don't know how


01-08-2009, 06:54 AM
You can do the same thing mentioned in your code through the following code also.

function checkingMyNewComm(opt){
var arr = [];
var firstparam = ['Profile','Blog','Photo','Music','Video','Classified','Guestbook'];
for(var i = 0; i < firstparam.length; ++i){
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

01-08-2009, 06:59 AM
hum... let me think

01-08-2009, 07:01 AM
does the value of "return comm_profile+comm_blog+comm_photo+comm_music+comm_video+comm_classified+comm_guestbook;" same as "return arr.join(' ');" ?

01-08-2009, 07:20 AM
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 (https://developer.mozilla.org/en/Core_JavaScript_1.5_Reference/Global_Objects/Array/join) to learn more about the join method associated with the Array object.

01-08-2009, 07:43 AM
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

01-08-2009, 07:51 AM
Hey, by the way thanks for explanation. You're really help

01-08-2009, 08:15 AM
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);


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;

01-08-2009, 08:36 AM
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..

01-08-2009, 08:49 AM
Well first of all, in this case - the code for this thread, I got the impression that arithmetic addition of numbers was required. That's what both of my code suggestions do.

As to the practice of joining an array of strings, as opposed to concatenating using the plus (+) sign a series of strings, it is my impression that using the join() method and an array is thought to be faster. I've never tested this, but it makes sense. The join() method will work in one pass, using the plus sign requires several or more operations in a row.

I also think that doing it with join() looks cooler, but that is of no importance, right? :)

01-08-2009, 09:18 AM
I've tested the speed comparison sometime back and at that time the join based method passed with flying colors against the normal string concatenation method.

So I've been trying to avoid the string concatenation if the number of strings involved varies.