CodeNewbie Community 🌱

Cover image for Using JavaScript's Slice Method to Carve a Turducken
Chris Jarvis
Chris Jarvis

Posted on • Originally published at dev.to

Using JavaScript's Slice Method to Carve a Turducken

It's Thanksgiving time in the US. So lets Slice the bird. This year we're not settling for a turkey, lets go for the excessive Turducken. A Turducken is a deboned chicken cooked inside a deboned duck that is cooked inside a deboned turkey.

Ingredients

First gather all the ingredients or variables.


let bird1 = "Turkey"; 
let bird2 = "Duck";
let bird3 = "Chicken";

Enter fullscreen mode Exit fullscreen mode

Slice

We use a basic JavaScript slice to carve the turkey. The slice() method extracts a section of a string and returns it as a new string. Use parameters to specify the part of string to be extracted.

The parameters are the start point and the end point, The slice happens before the endpoint. That character is not included in the new string.

The first character is at position zero, the rest are off by one. To make a Turducken we need to remove the first three letters from Turkey or Position 0 to 3 wrote as bird1.slice(0, 3).

let bird1 = "Turkey";

let carveFirstBird = bird1.slice(0, 3);

Enter fullscreen mode Exit fullscreen mode

Will yield "Tur."

Next we need the Duck. We want the whole duck, or whole string, so we only need the first parameter. If you don't add an endpoint the whole string will be sliced.

bird2.slice(0);

let bird2 = "Duck";
let carveSecondBird2 = bird2.slice(0).toLowerCase();

Enter fullscreen mode Exit fullscreen mode

This will give us the "duck"

Finally we need the chicken but how do we get the end of the string, negative numbers. to get the 'en' we slice like this, bird3.slice(-2) This will slice the string starting at two characters from the end.

let bird3 = "Chicken"; 
let carveThirdBird = bird3.slice(-2);
Enter fullscreen mode Exit fullscreen mode

We have the "en" from Chicken. Now to put them together.

Combine Ingredients

Make dinner by putting it all the birds together.


let dinner = carveFirstBird + carveSecondBird + carveThirdBird;
document.write(dinner); // Turducken
Enter fullscreen mode Exit fullscreen mode

This give us Turducken.

Recipe

let bird1 = "Turkey"; 
let bird2 = "Duck";|
let bird3 = "Chicken";

let carveFirstBird = bird1.slice(0, 3);
let carveSecondBird = bird2.slice(0).toLowerCase();  
let carveThirdBird = bird3.slice(-2);

let dinner = carveFirstBird + carveSecondBird + carveThirdBird;
 document.write(dinner);
Enter fullscreen mode Exit fullscreen mode

If you love that, try this

There's also a Gooducken. This recipe replaces the turkey with a goose. For this example we can use another way to get the end of the string for the dinner.
We slice bird1 the same as above. Instead of using the whole string for bird2 we only get the first three letters. So we slice the duck the same way as we did for Goose.


let bird1 = "Goose"; 
let bird2 = "Duck";
let bird3 = "Chicken";

let carveFirstBird = bird1.slice(0, 3);
let carveSecondBird2 = bird2.slice(0, 3).toLowerCase();


Enter fullscreen mode Exit fullscreen mode

Then how do we get the "K" we pull it from the chicken but use another way. We slice the fourth position, but fifth character, and just go to the end of the string. bird3.slice(4); If we don't add an endpoint it will slice everything starting at the start point.


let carveThirdBird = bird3.slice(4);
Enter fullscreen mode Exit fullscreen mode

gives us "ken". Dinner goes together same as before.


let dinner = carveFirstBird + carveSecondBird + carveThirdBird;

Enter fullscreen mode Exit fullscreen mode

Gives us Gooducken.

That's how to carve with JavaScript. I hope you enjoyed this look at Slice. No, I have never tried a turducken in real life.

Top comments (0)