Permalink
Browse files

Added ECMAScript 5 solution

  • Loading branch information...
1 parent 8164973 commit fac880c4d3a182296c4cc6dc6c86713fd848a4cf @ialcazar committed Aug 13, 2011
View
@@ -2,4 +2,5 @@ Contains some JavaScript arrays examples:
- concatenator: Create a funciton that given an array, that contains strings and arrays, concatenate every String and split into commas. E.g. If array is ["hola", ["soy", ["juan", "fernandez"] ], "y", ["no", "tengo", ["dinero"] ] ] it will return "hola,soy,juan,fernandez,y,no,tengo,dinero"
-- advancedConcatenator: Just a more advance solution
+- advancedConcatenator: Just a more advance solution
+- advancedConcatenatorECMASCRIPT5: Same solution without overwriting Array.prototype
@@ -0,0 +1,40 @@
+//In ECMAScript 5 exists this:
+//Array.isArray()
+//so I don't need to modify Array.prototype
+
+var concatenate = (function(){
+ //Private properties
+ var params;
+
+
+ function hasElements(){
+ return params.length>0;
+ }
+
+ function createString(){
+ var res = "";
+ for(var i=0;i<params.length;i++){
+ res += params[i];
+ if(isLastParam(i)){
+ res+=",";
+ }
+ }
+ return res;
+ }
+ function isLastParam(p1){
+ return (p1<params.length-1);
+ }
+
+ //Public
+ return function (parameters){
+ var result = "";
+ params = parameters && parameters.isArray()?parameters:[ ];
+
+ if(hasElements()){
+ result = createString();
+ }
+ return result;
+ }
+})();
+
+

0 comments on commit fac880c

Please sign in to comment.