Cara menggunakan alternative to splice javascript
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
Provided Test Cases
PEDACUnderstanding 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 First let’s address .splice(start, deleteCount, item1, item2, item3, etc.) 7:
"Bastian".slice() 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() 0:
We can pass "Bastian".slice() 3 is a number that tells .splice() at which index to begin deleting elements. "Bastian".slice() 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() 5 to zero. Now we can start adding items. Just separate each element with a comma, like so "Bastian".slice() 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() 9 into .splice(start, deleteCount, item1, item2, item3, etc.) 0. We want to insert each element of "Bastian".slice() 9 into .splice(start, deleteCount, item1, item2, item3, etc.) 0.Algorithm:
Code: See below! Without comments: Solution #2: .slice( ), .splice( ), and for loopPEDACUnderstanding 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 First let’s address .splice(start, deleteCount, item1, item2, item3, etc.) 7:
"Bastian".slice() 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() 0:
We can pass "Bastian".slice() 3 is a number that tells .splice() at which index to begin deleting elements. "Bastian".slice() 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() 5 to zero. Now we can start adding items. Just separate each element with a comma, like so "Bastian".slice() 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() 9 and insert them into .splice(start, deleteCount, item1, item2, item3, etc.) 0.The trick here is to increment "Bastian".slice() 9 will not end up in the right order when inserted into .splice(start, deleteCount, item1, item2, item3, etc.) 0. |