Welcome to the Treehouse Community

The Treehouse Community is a meeting place for developers, designers, and programmers of all backgrounds and skill levels to get support. Collaborate here on code errors or bugs that you need feedback on, or asking for an extra set of eyes on your latest project. Join thousands of Treehouse students and alumni in the community today. (Note: Only Treehouse students can comment or ask questions, but non-students are welcome to browse our conversations.)

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and a supportive community. Start your free trial today.

JavaScript JavaScript Foundations Arrays Methods: Part 3

Tammy Herrera
Tammy Herrera
4,070 Points

On about line 19, set the variable 'shortSaying' to be a slice of the 'saying' array to include the strings of "quick",

"brown", "fox".

var first = ["The", "quick", "brown", "fox", "jumps"]; var second = ["over", "the", "lazy"]; var saying = first.concat("over", "the", "lazy", "dog");

var shortSaying = saying;

can someone please solve this? I'm not coming up with the correct result..this apparently is the incorrect answer:

var shortSaying = saying.slice("quick", "jumps");

5 Answers

Hi Tammy,

First off, I see what you did for saying and that works but there's an easier way var saying = first.concat (second, "dog"); So all the elements from first will be concatenated with all of the elements from second and "dog"

For the slice method, you have the right idea. You want to start with "quick" and go up to but not including "jumps" except that the slice method accepts indexes and not array values. So replace those 2 strings with what their index would be.

Tammy Herrera
Tammy Herrera
4,070 Points

Can you explain what their index means, please?

Sure.

After the concatenation process the saying array will look like this: ["The", "quick", "brown", "fox", "jumps", "over", "the", "lazy", "dog"]

Now, they want you to get a slice of that array from "quick" to "fox".

array indexes start at 0. So the array index for "The" is 0. Element numbering starts at 1. So "The" is element number 1 and it is at index 0. This is just the terminology.

So if you wanted to access the first element, "The", you would do saying[0] That would evaluate to "The"

Getting back to the slice method, the 1st parameter to slice is the index of where you want to start the slice. Since we want to start at "quick" we need that index number, It would be 1 since index numbering starts at 0.

So we have var shortSaying = saying.slice(1); The second argument is where you should end it. This second index isn't included in the slice so you actually have to go one greater than where you want to stop. Since we want to stop at "fox" we need the index of "jumps" which would be 4 if you start counting from 0 at the beginning.

var shortSaying = saying.slice(1, 4);

Francesco Vittorio Beninato
Francesco Vittorio Beninato
3,624 Points

Hi guys! I don't wanna be annoying but why is it saying.slice(1,4); wasn't correct (1,3) since the description above the exercise?

Thanks in advance for your help

Hi Francesco,

The second argument is the index of where you want to stop. This index is not included in the slice. So when you do .slice(1, 4) the slice is going to include the elements at index 1, 2, and 3 but not 4. This gives you 3 elements total.

Let me know if that clears it up for you.

Francesco Vittorio Beninato
Francesco Vittorio Beninato
3,624 Points

Ok!

Now it's totally clear!!

Thanks a lot Jason

Francesco Beninato