Created
August 16, 2011 12:00
-
-
Save javierbrea/1148923 to your computer and use it in GitHub Desktop.
Solucion al ejercicio Concatenator #gejs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
var example=["hola",3,["soy",function(){}, ["juan", "fernandez"] ], "y",new Date(), ["no", "tengo",function(){},["dinero",[]] ] ]; | |
/* Using .isArray ECMAS5 | |
https://developer.mozilla.org/en/JavaScript/Reference/Global_Objects/Array/isArray */ | |
if(!Array.isArray) { | |
Array.isArray = function (arg) { | |
return Object.prototype.toString.call(arg) === '[object Array]'; | |
}; | |
} | |
var concatena=function(obj,character){ | |
character = character || ','; | |
var result='',partialResult,concatenator; | |
if(typeof(obj)=='string'){ | |
return obj; | |
} | |
if (Array.isArray(obj)){ | |
for(var i=0,z=obj.length;i<z;i++){ | |
partialResult=concatena(obj[i],character); | |
concatenator = i>0 ? character : ''; | |
if(partialResult!=''){ | |
result += concatenator+partialResult; | |
} | |
} | |
} | |
return result; | |
} | |
concatena(example,'-'); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment