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
Kristian Woods
23,414 PointsWhy I am having an issue looping through this whole array and returning the values back to the function?
I have a function that loops an array - I want to be able to output the values using different methods e.g. console.log(), alert() etc. However, I can only get the function to output one item from the array (usually the first or last item).
let fruitArray = [
'pear',
'apple',
'strawberry'
];
let loopArray = (arr) => {
for(let i = 0; i < arr.length; i++) {
let arrayItem = arr[i];
console.log(arrayItem);
}
};
The above function loops the array and stores the values in a variable, I then console.log that value. This is limiting, however. So I tried a different method:
let loopArray = (arr) => {
for(let i = 0; i < arr.length; i++) {
let arrayItem = arr[i];
return arrayItem;
}
};
console.log(loopArray(fruitArray));
// this results in 'pear' from the array. BUT not the rest of the array...
I thought this would work? But I then remembered that the return statement ends the function. However, I still don't have a solution. Can someone shed some light on this for me?
Thanks in advance
2 Answers
William Tan
5,148 Pointslet fruitArray = [
'pear',
'apple',
'strawberry'
];
let loopArray = (arr) => {
for(let i = 0; i < arr.length; i++) {
console.log(arr[i])
}
};
loopArray(fruitArray);
If there is a return statement within the for loop {code block}, return breaks the loop.
Kristian Woods
23,414 PointsHey, William and Zack, thank you for getting back to me.
William, your code example helps. But I'm looking to be able to output the value of the loopArray function WITHOUT explicitly console.log() - this way, I can output the value different ways. Your method is explicitly using console.log() to output the data.
Zack Lee
Courses Plus Student 17,662 PointsYou could store the value in another variable like this:
Var newList = loopArray(fruitArray);
Console.log is just a way of seeing the data formatted. You can store the output of the function in another variable if you like though. Or use a push method in your loop to push each iteration to an array variable
Zack Lee
Courses Plus Student 17,662 PointsZack Lee
Courses Plus Student 17,662 PointsYour first block is storing the function in the loopArray variable. you have to then initiate that function by doing something like you did in the second block: console.log(loopArray(fruitArray); William is correct, the return statement terminates the loop after the first iteration.