Cara menggunakan alternative to splice javascript

.slice() and .splice() are similar methods in JavaScript. They look similar, they sound similar, and they do similar things. For those reasons, it’s important to know the differences between them. Also, they’re used very often, so understanding their usage is good to learn early on for any software developer.

In this article we’ll look at how to use them with a specific algorithm scripting challenge. We’ll be inserting elements from one array into another and returning the combined array without mutating the original arrays.

Algorithm instructions

You are given two arrays and an index.

Use the array methods slice and splice to copy each element of the first array into the second array, in order.

Begin inserting elements at index n of the second array.

Return the resulting array. The input arrays should remain the same after the function runs.

Provided Test Cases

  • frankenSplice([1, 2, 3], [4, 5], 1) should return
    .splice(start, deleteCount, item1, item2, item3, etc.)
    0.
  • .splice(start, deleteCount, item1, item2, item3, etc.)
    1 should return
    .splice(start, deleteCount, item1, item2, item3, etc.)
    2.
  • .splice(start, deleteCount, item1, item2, item3, etc.)
    3 should return
    .splice(start, deleteCount, item1, item2, item3, etc.)
    4.
  • All elements from the first array should be added to the second array in their original order.
  • The first array should remain the same after the function runs.
  • The second array should remain the same after the function runs.
Solution #1: .slice( ), .splice( ), and spread operator

PEDAC

Understanding the Problem: We have one input, a string. Our output is also a string. Ultimately, we want to return the input string with the first letter — and only the first letter — of each word capitalized.

Examples/Test Cases: Our provided test cases show that we should have a capitalized letter only at the beginning of each word. We need to lower case the rest. The provided test cases also show that we aren’t being thrown any curve balls in terms of weird compound words separated by symbols instead of whitespace. That’s good news for us!

Data Structure: We are going to have to transform our input string into an array in order to manipulate each word separately.

Let’s have a little chat about .slice() and .splice():

First let’s address

.splice(start, deleteCount, item1, item2, item3, etc.)
7:

.slice() extracts a section of a string and returns it as a new string. If you call .slice() on a string without passing it any additional information, it will return the whole string.

"Bastian".slice()
// returns "Bastian"

This will be useful to us in this algorithm scripting challenge because the instructions tell us that we should not directly modify the input arrays. So we’re going to need to make a copy of one of them.

Now let’s look at

"Bastian".slice()
// returns "Bastian"
0:

.splice() changes the contents of an array by removing or replacing existing elements and/or adding new elements.

We can pass .splice() several arguments that determine where the deletion begins, how much is deleted, and what is inserted.

"Bastian".slice()
// returns "Bastian"
3 is a number that tells .splice() at which index to begin deleting elements.
"Bastian".slice()
// returns "Bastian"
5 tells .splice() how many elements to delete.

Wait a second! What if you don’t want to delete anything? What if you just want to insert elements? That’s fine. Just set

"Bastian".slice()
// returns "Bastian"
5 to zero. Now we can start adding items. Just separate each element with a comma, like so
"Bastian".slice()
// returns "Bastian"
8.

.splice(start, deleteCount, item1, item2, item3, etc.)

Another concept to keep in mind for this algorithm scripting challenge is the spread operator. ES6 gifted us with the spread operator which looks like ellipses — just three dots in a row.

The spread operator is most commonly used when you want to use the elements of an array as arguments to a function. That’s exactly what we’re going to do with it in this challenge. We don’t want to insert the entire array

"Bastian".slice()
// returns "Bastian"
9 into
.splice(start, deleteCount, item1, item2, item3, etc.)
0. We want to insert each element of
"Bastian".slice()
// returns "Bastian"
9 into
.splice(start, deleteCount, item1, item2, item3, etc.)
0.

Algorithm:

  1. Create a copy of
    .splice(start, deleteCount, item1, item2, item3, etc.)
    0.
  2. Insert all the elements of
    "Bastian".slice()
    // returns "Bastian"
    9 into
    .splice(start, deleteCount, item1, item2, item3, etc.)
    0 starting at the index in
    .splice(start, deleteCount, item1, item2, item3, etc.)
    0 specified by n.
  3. Return the combined arrays.

Code: See below!

Without comments:

Solution #2: .slice( ), .splice( ), and for loop

PEDAC

Understanding the Problem: We have one input, a string. Our output is also a string. Ultimately, we want to return the input string with the first letter — and only the first letter — of each word capitalized.

Examples/Test Cases: Our provided test cases show that we should have a capitalized letter only at the beginning of each word. We need to lower case the rest. The provided test cases also show that we aren’t being thrown any curve balls in terms of weird compound words separated by symbols instead of whitespace. That’s good news for us!

Data Structure: We are going to have to transform our input string into an array in order to manipulate each word separately.

Let’s have a little chat about .slice() and .splice():

First let’s address

.splice(start, deleteCount, item1, item2, item3, etc.)
7:

.slice() extracts a section of a string and returns it as a new string. If you call .slice() on a string without passing it any additional information, it will return the whole string.

"Bastian".slice()
// returns "Bastian"

This will be useful to us in this algorithm scripting challenge because the instructions tell us that we should not directly modify the input arrays. So we’re going to need to make a copy of one of them.

Now let’s look at

"Bastian".slice()
// returns "Bastian"
0:

.splice() changes the contents of an array by removing or replacing existing elements and/or adding new elements.

We can pass .splice() several arguments that determine where the deletion begins, how much is deleted, and what is inserted.

"Bastian".slice()
// returns "Bastian"
3 is a number that tells .splice() at which index to begin deleting elements.
"Bastian".slice()
// returns "Bastian"
5 tells .splice() how many elements to delete. Wait a second! What if you don’t want to delete anything? What if you just want to insert elements? That’s fine. Just set
"Bastian".slice()
// returns "Bastian"
5 to zero. Now we can start adding items. Just separate each element with a comma, like so
"Bastian".slice()
// returns "Bastian"
8.

.splice(start, deleteCount, item1, item2, item3, etc.)

Unlike in the previous solution, we won’t be using the spread operator here. We’ll be using a for loop instead to pluck each element one at a time from

"Bastian".slice()
// returns "Bastian"
9 and insert them into
.splice(start, deleteCount, item1, item2, item3, etc.)
0.

The trick here is to increment n by 1 each time the loop runs or else the elements of

"Bastian".slice()
// returns "Bastian"
9 will not end up in the right order when inserted into
.splice(start, deleteCount, item1, item2, item3, etc.)
0.