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 trialVioleta Last
14,547 PointsWhat is the length value of the alltasks array after the above code runs?
Consider the following code:
const morningTasks = [ 'study', 'exercise', 'write' ];
const eveningTasks = [ 'bake', 'edit article', 'chill' ];
const allTasks = [...morningTasks, ...eveningTasks];
morningTasks.pop();
What is the length value of the allTasks array after the above code runs?
so I counted is as 5 as allTasks combines 2 list with 3 elements in each so length would be 5. Now the morningTasks.pop(); runs after allTask list is created and only affects the morningTask list not the allTasks list as it is already formed , correct (what am I missing?)?
1 Answer
Rohald van Merode
Treehouse StaffHey Violeta Last 👋
Your explanation is spot on! The allTasks
variable is being created before the number of values in morningTasks
is being changed and its length therefor won't change.
The length of allTasks
however is not 5 but 6 🙂 Both morning and evening task arrays contain 3 values, which makes 6 tasks in total.
Hope this clears things up! 😃
Violeta Last
14,547 PointsVioleta Last
14,547 Pointsyes but the js count starts at 0 so the length value would be 0,1,2,3,4,5 ... right?
Rohald van Merode
Treehouse StaffRohald van Merode
Treehouse StaffGreat question Violeta Last! Arrays are indeed 0 indexed, when looping over the items in an array or trying to access a specific item from that array you'll indeed want to keep that in mind.
In this case however you're not working with any indexes. The length property returns a number that represents the number of elements in that array.