From b0ec16b93968d190a12d6d524ca7ee11311c55ed Mon Sep 17 00:00:00 2001 From: Parmeet Singh Date: Sun, 4 Oct 2020 16:25:06 +0530 Subject: [PATCH 1/3] Add files via upload --- arrays/Slice.md | 47 +++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 47 insertions(+) create mode 100644 arrays/Slice.md diff --git a/arrays/Slice.md b/arrays/Slice.md new file mode 100644 index 00000000..22470c4d --- /dev/null +++ b/arrays/Slice.md @@ -0,0 +1,47 @@ +# Slice + +You have an array of elements and you want to access or require elements upto a certain index. So rather than looping in down to the whole array, we can use inbuilt **slice** method. This method consists of 2 optional parameters (start and end). Let us consider some examples to understand more. + +**WITH ONE PARAMTER** + + //An array + var countries = ['India', 'United States of America', 'United Kingdom', 'Canada', 'Australia']; + + //console.log to see the effect + console.log(countries.slice(2)); + + //Output will be - ['United Kingdom', 'Canada', 'Australia']; + +When you provide a single parameter in slice method, it will start picking the elements from that parameter (index starts from zero) and prints till the end. + +Now let's see what happend if we pass **BOTH THE PARAMETERS** + + //Same Array + var countries = ['India', 'United States of America', 'United Kingdom', 'Canada', 'Australia']; + + console.log(countries.slice(1,4)); + + //Output: ['United States of America', 'United Kingdom', 'Canada']; + +When you pass both the paramter (start and end), slice will start picking elements from 'start' index and prints till 'end-1' index. As you can see from above example (it prints countries from index 1 to 3). + +As I said, these both parameters are **optional**, what would happen if you do not pass any parameter. + + //Same array + var countries = ['India', 'United States of America', 'United Kingdom', 'Canada', 'Australia']; + + console.log(countries.slice()); + + //Output: ['India', 'United States of America', 'United Kingdom', 'Canada', 'Australia']; + +In that case, it will print your whole array. + +You can assign a new variable to store the results from slice. + + //Same array + var countries = ['India', 'United States of America', 'United Kingdom', 'Canada', 'Australia']; + + var newArray = countries.slice(1,4); + console.log(newArray); + + //Output: ['United States of America', 'United Kingdom', 'Canada']; From 95004d1c0f8be3eed3b820ab2b51c886b467bdd1 Mon Sep 17 00:00:00 2001 From: Parmeet Singh Date: Sun, 4 Oct 2020 16:31:39 +0530 Subject: [PATCH 2/3] Update Slice.md --- arrays/Slice.md | 49 +++++++++++++++++++++---------------------------- 1 file changed, 21 insertions(+), 28 deletions(-) diff --git a/arrays/Slice.md b/arrays/Slice.md index 22470c4d..c0061d62 100644 --- a/arrays/Slice.md +++ b/arrays/Slice.md @@ -3,45 +3,38 @@ You have an array of elements and you want to access or require elements upto a certain index. So rather than looping in down to the whole array, we can use inbuilt **slice** method. This method consists of 2 optional parameters (start and end). Let us consider some examples to understand more. **WITH ONE PARAMTER** - - //An array - var countries = ['India', 'United States of America', 'United Kingdom', 'Canada', 'Australia']; - - //console.log to see the effect - console.log(countries.slice(2)); +```javascript +//An array +var countries = ['India', 'United States of America', 'United Kingdom', 'Canada', 'Australia']; - //Output will be - ['United Kingdom', 'Canada', 'Australia']; +//console.log to see the effect +console.log(countries.slice(2)); +//Output will be - ['United Kingdom', 'Canada', 'Australia']; +``` + When you provide a single parameter in slice method, it will start picking the elements from that parameter (index starts from zero) and prints till the end. Now let's see what happend if we pass **BOTH THE PARAMETERS** - //Same Array - var countries = ['India', 'United States of America', 'United Kingdom', 'Canada', 'Australia']; - - console.log(countries.slice(1,4)); - - //Output: ['United States of America', 'United Kingdom', 'Canada']; +```javascript +console.log(countries.slice(1,4)); +//Output: ['United States of America', 'United Kingdom', 'Canada']; +``` When you pass both the paramter (start and end), slice will start picking elements from 'start' index and prints till 'end-1' index. As you can see from above example (it prints countries from index 1 to 3). As I said, these both parameters are **optional**, what would happen if you do not pass any parameter. - - //Same array - var countries = ['India', 'United States of America', 'United Kingdom', 'Canada', 'Australia']; - - console.log(countries.slice()); - - //Output: ['India', 'United States of America', 'United Kingdom', 'Canada', 'Australia']; +```javascript +console.log(countries.slice()); +//Output: ['India', 'United States of America', 'United Kingdom', 'Canada', 'Australia']; +``` In that case, it will print your whole array. You can assign a new variable to store the results from slice. - - //Same array - var countries = ['India', 'United States of America', 'United Kingdom', 'Canada', 'Australia']; - - var newArray = countries.slice(1,4); - console.log(newArray); - - //Output: ['United States of America', 'United Kingdom', 'Canada']; +```javascript +var newArray = countries.slice(1,4); +console.log(newArray); +//Output: ['United States of America', 'United Kingdom', 'Canada']; +``` From bffd2ad7b633bb1dc2b9133fdafa60373580c54f Mon Sep 17 00:00:00 2001 From: Parmeet Singh Date: Sun, 4 Oct 2020 16:33:13 +0530 Subject: [PATCH 3/3] Update Slice.md --- arrays/Slice.md | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/arrays/Slice.md b/arrays/Slice.md index c0061d62..d3fb013a 100644 --- a/arrays/Slice.md +++ b/arrays/Slice.md @@ -2,7 +2,7 @@ You have an array of elements and you want to access or require elements upto a certain index. So rather than looping in down to the whole array, we can use inbuilt **slice** method. This method consists of 2 optional parameters (start and end). Let us consider some examples to understand more. -**WITH ONE PARAMTER** +1. **WITH ONE PARAMTER** ```javascript //An array var countries = ['India', 'United States of America', 'United Kingdom', 'Canada', 'Australia']; @@ -15,7 +15,7 @@ console.log(countries.slice(2)); When you provide a single parameter in slice method, it will start picking the elements from that parameter (index starts from zero) and prints till the end. -Now let's see what happend if we pass **BOTH THE PARAMETERS** +2. Now let's see what happend if we pass **BOTH THE PARAMETERS** ```javascript console.log(countries.slice(1,4)); @@ -24,7 +24,7 @@ console.log(countries.slice(1,4)); When you pass both the paramter (start and end), slice will start picking elements from 'start' index and prints till 'end-1' index. As you can see from above example (it prints countries from index 1 to 3). -As I said, these both parameters are **optional**, what would happen if you do not pass any parameter. +3. As I said, these both parameters are **optional**, what would happen if you do not pass any parameter. ```javascript console.log(countries.slice()); //Output: ['India', 'United States of America', 'United Kingdom', 'Canada', 'Australia'];