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 Foundations Arrays Methods: Part 3

Stuck on JavaScript slice method

Hi This is my code i'm stuck on this and i'm 9.99% that is right but what i'm my missing any help will be great.

var first =  ["The", "quick", "brown", "fox", "jumps"]
      var second =  ["over", "the", "lazy"];
      var saying = first; 
        var shortSaying = saying.slice(1,3);//Stuck on!!
      var saying = first.concat(second).concat("dog");
      var shortSaying = saying;
      var sayingString = saying;

For readability: (only whitespace was changed)

var first = ["The", "quick", "brown", "fox", "jumps"]
var second = ["over", "the", "lazy"];
var saying = first;
var shortSaying = saying.slice(1,3);

// Stuck on!!
var saying = first.concat(second).concat("dog");

var shortSaying = saying;
var sayingString = saying;

(if you want to format your code like I did there, check out the Markdown Cheatsheet)

5 Answers

In that case, your code is in the wrong order. You're getting index 1-3 of what's essentially the first variable.

Try this code instead:

var first = ["The", "quick", "brown", "fox", "jumps"]
var second = ["over", "the", "lazy"];
var saying = first.concat( second ).concat( "dog" ); // this line used to be below
var shortSaying = saying.slice( 1, 4 ); // this line used to above
var sayingString = saying;

Also, in this code challenge, there is no need to add any extra lines of code with new variable assignments.

First, I notice that your first statement doesn't have a semicolon at the end. I'm not sure if that's it, but it very well could be. ;)

I'll update this comment after I do some more digging.

EDIT: In this code challenge, there isn't any need to define any new variables. Your "stuck on" line of code is actually great, but you've written it on the wrong line.

Try that and see if it helps.

well i'm not stuck on the dog i'm stuck on the one that you got to slice the quick brown fox but i'm 9.99% the numbers that are the index ps how do i but my code in as markup ?

To make your code markup, wrap it in a couple newlines and three backticks (`). Look at the Markdown Cheatsheet.

Well, in the case of .slice(), you're close. You see, the .slice() method stops before the index of its second argument.

It's like if I told you to count from 0 to 5, and told you to stop before 5. You'd stop at 4.

This is the same way. It's counting from index 1 (actually the second element) to index 3 (actually the fourth element), but .slice() tells it to stop before index 3. So, to make up for that, you'll need to pass 4 instead.

so i would have to change it like this

var first =  ["The", "quick", "brown", "fox", "jumps"]
      var second =  ["over", "the", "lazy"];
      var saying = first; 
        var shortSaying = saying.slice(1,4);//would this work because i tried it not long ago   and it's saying wrong.
      var saying = first.concat(second).concat("dog");
      var shortSaying = saying;
      var sayingString = saying;

Thank you Ryan Duchene for your help :).

No problem!