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

Code Challenge: Methods Part 2 problem

Hi,

I'm not able to pass this second test:

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

This is my answer but nothing works.

 <script>
      var first =  ["The", "quick", "brown", "fox", "jumps"]
      var second =  ["over", "the", "lazy"];
      var saying = first.concat(["over", "the", "lazy", "dog"]); 
      var shortSaying = saying.slice(["quick", "jumps"]);
      var sayingString = saying;
    </script> 

This is the answer I get when I check my work:

Bummer! The 'shortSaying' should be 'quick,brown,fox' not 'The,quick,brown,fox,jumps,over,the,lazy,dog'

There seems to be no explanation on how to do this in the method 2 video. I tried alot of things and I really d'ont know what i'm doing wrong ?

4 Answers

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

You have to use numbers to index the strings

Jacob Miranda
Jacob Miranda
19,392 Points

The slice function will take numbers as an argument. The first number we can call X and the second, Y (x,y).

X will be the beginning of where you want to slice the array and Y will be the end.

You can look at the array like this >>

var first = [ 0 "The", 1 " quick", 2 "brown", 3 "fox", 4 "jumps" 5] .

If you want to slice quick brown, you will need to start at 1 and end at 3.

Play around with the numbers to see how it works and what you're actually slicing as a result. Happy coding.

Booya! It works!

Thanks you very much Jacob !

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

I seem to be having a different issue, I am 99.9% sure this is correct but its still not passing. It just says

"Bummer! The 'shortSaying' should be 'quick,brown,fox' not 'quick'" but, its not seeing the other ,2 ,3

Paul Bennett, you don't need to list every number, just the beginning and ending. var shortSaying = saying.slice(1, 4);