Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
ejercicio1 jornada 3 grupo estudio java script
// Crear una funcion que dado un array, que puede contener strings o arrays,
// concatene todos los strings y los separe por comas. Ej. Si el array que
// entra es:
// ["hola", ["soy", ["juan", "fernandez"]], "y", ["no", "tengo", ["dinero"]]]
// Devolvera la cadena "hola,soy,juan,fernandez,y,no,tengo,dinero"
function concatenator(initialArray, separator){
separator = separator || ",";
var elem,
i,
size,
resArray = [],
size = initialArray.length;
for (i=0; i < size; i++) {
elem = initialArray[i];
elem = Array.isArray(elem)?concatenator(elem, separator):elem;
resArray.push(elem);
}
return resArray.join(separator);
}
console.log("Tests");
console.assert("" === concatenator([""]), "Cadena vacia");
console.assert("hola" === concatenator(["hola"]), "Un elemento");
console.assert("hola,esto,es,una,prueba" ===
concatenator(["hola", "esto", "es", "una", "prueba"]), "Solo strings");
console.assert("hola,soy,juan,fernandez,y,no,tengo,dinero" ===
concatenator(["hola", ["soy", ["juan", "fernandez"]], "y", ["no", "tengo", ["dinero"]]]),
"Con Arrays internos");
console.assert("hola;soy;juan;fernandez;y;no;tengo;dinero" ===
concatenator(["hola", ["soy", ["juan", "fernandez"]], "y", ["no", "tengo", ["dinero"]]], ";"),
"Con Arrays internos y separador");
console.log("Tests OK");
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.