Welcome to the Treehouse Community
Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.
Looking to learn something new?
Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.
Start your free trial
michael delgado
993 Pointshow can i make a function return the contents of an array? ex. function print(array)
i want to write a function called print that will return the variables in an array to the console. i have this so far:
function print(array, instructions) { var output = []; for (var i=0; i<array.length;i++) { output.push(instructions(array[i])); } return output; }
.....im not sure what to do.
2 Answers
Nure México
25,840 Pointsfunction print() {
for (var i = 0; i < arguments.length; i++) {
console.log(arguments[i])
};
}
//Example
print("12",23,"Hola",12,44)
Nure México
25,840 PointsI think that’s it
Gabriel D. Celery
13,810 PointsHere is a quick fix for your code:
var arrayToConvert = ["12",23,"Hola",12,44];
function print(parameter) {
for (var i = 0; i < parameter.length; i++) {
console.log(parameter[i])
};
}
print(arrayToConvert);
michael delgado
993 Points@César Guadarrama Cantú
Thank you. i see from your resposne that I forgot For.
Gabriel D. Celery Thank you. I noticed how you created a Variable to understand its place in the function
Gabriel D. Celery
13,810 PointsGabriel D. Celery
13,810 PointsCan you use an example to explain what exactly do you want to achieve? For example you want to convert a var array = [0,1,2,3] into what?