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 Splice

Craig Jamieson
Craig Jamieson
19,585 Points

Use the 'splice' method to replace "quick" and "brown" from the 'saying' array and add "slow" and "red" in their place i

It says ... Use the 'splice' method to replace "quick" and "brown" from the 'saying' array and add "slow" and "red" in their place in that order.

My code is

saying.splice(1,2,["slow","red"]);```
But it fails and says "
Bummer! Expecting the array to be 'The,slow,red,fox,jumps,over,the,lazy,dog' but got 'The,slow,red,fox,jumps,over,the,lazy,dog'"

However I have aligned that and they match

The,slow,red,fox,jumps,over,the,lazy,dog
The,slow,red,fox,jumps,over,the,lazy,dog

So I don't understand what I'm doing wrong?

2 Answers

Right now, your code is inserting another array inside the saying array:

["The", ["slow", "red"], "fox", "jumps", "over", "the", "lazy", "dog"]

Array.prototype.splice() takes as many parameters as needed, but not in a from of an array (if you pass it an array, it will insert that array as a single element of the array the method was called on). If you want to add more than one new element, you just add more parameters to splice, like this:

saying.splice(1, 2, "slow", "red");
Roberto Alicata
PLUS
Roberto Alicata
Courses Plus Student 39,959 Points

you have to pass a sequence of string not an array:

saying.splice(1,2,"slow","red");