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

JavaScript JavaScript and the DOM (Retiring) Getting a Handle on the DOM Practice Selecting Elements

Brent Mickle
Brent Mickle
2,480 Points

How do I reference multiple items in an array??

If the array has an index of 5 and I only want to reference the first 3 only. [0-2] for example. Can this be done in one line of code to give to a single variable? I am supposed to reference 3 links in the <nav> portion of the html code but the links are grandchildren of the <nav> tag. like so: <nav><ul><li>. Any help is appreciated.

2 Answers

Antonio De Rose
Antonio De Rose
20,884 Points

yes, you can like the below

var cars = ["Benz", "BMW", "Honda", "Audi", "Toyota"];
alert(cars.slice(0, 3));

//you are telling the browser, to consider the array from the starting position, by mentioning, zero
//as the first parameter

//and the second parameter, you are mentioning to take 3 elements, starting from the zero'th index.

//try run the below.
//https://jsfiddle.net/82nLf6pz/3/
Brent Mickle
Brent Mickle
2,480 Points

Did you happen to check out my challenge that this question is based on?? I tried what you said but still come up short. I appreciate the input

Antonio De Rose
Antonio De Rose
20,884 Points

yeap, all of 'em

what answer, for what question did you hit, give me details.