
- Javascript Basics Tutorial
- Javascript - Home
- Javascript - Overview
- Javascript - Syntax
- Javascript - Enabling
- Javascript - Placement
- Javascript - Variables
- Javascript - Operators
- Javascript - If...Else
- Javascript - Switch Case
- Javascript - While Loop
- Javascript - For Loop
- Javascript - For...in
- Javascript - Loop Control
- Javascript - Functions
- Javascript - Events
- Javascript - Cookies
- Javascript - Page Redirect
- Javascript - Dialog Boxes
- Javascript - Void Keyword
- Javascript - Page Printing
- JavaScript Objects
- Javascript - Objects
- Javascript - Number
- Javascript - Boolean
- Javascript - Strings
- Javascript - Arrays
- Javascript - Date
- Javascript - Math
- Javascript - RegExp
- Javascript - HTML DOM
- JavaScript Advanced
- Javascript - Error Handling
- Javascript - Validations
- Javascript - Animation
- Javascript - Multimedia
- Javascript - Debugging
- Javascript - Image Map
- Javascript - Browsers
- JavaScript Useful Resources
- Javascript - Questions And Answers
- Javascript - Quick Guide
- Javascript - Functions
- Javascript - Resources
Get n numbers from array starting from given point JavaScript
We have to write an array function (Array.prototype.get()) that takes in three arguments first, a number n, second is also a number, say m, (m <= array length-1) and second is a string that can have one of the two values − ‘left’ or ‘right’.
The function should return a subarray of the original array that should contain n elements starting from the index m, and in the specified direction like left or right.
For example −
// if the array is: const arr = [0, 1, 2, 3, 4, 5, 6, 7]; // and the function call is: arr.get(4, 6, 'right'); // then the output should be: const output = [6, 7, 0, 1];
So, let’s write the code for this function −
Example
const arr = [0, 1, 2, 3, 4, 5, 6, 7]; Array.prototype.get = function(num, ind, direction){ const amount = direction === 'left' ? -1 : 1; if(ind > this.length-1){ return false; }; const res = []; for(let i = ind, j = 0; j < num; i += amount, j++){ if(i > this.length-1){ i = i % this.length; }; if(i < 0){ i = this.length-1; }; res.push(this[i]); }; return res; }; console.log(arr.get(4, 6, 'right')); console.log(arr.get(9, 6, 'left'));
Output
The output in the console will be −
[ 6, 7, 0, 1 ] [ 6, 5, 4, 3, 2, 1, 0, 7, 6 ]
- Related Articles
- Get the max n values from an array in JavaScript
- Sort array of points by ascending distance from a given point JavaScript
- Returning an array containing last n even numbers from input array in JavaScript
- Name the rays and starting point in the given figure :"\n
- Return the greatest possible product of n numbers from the array in JavaScript
- Retrieving n smallest numbers from an array in their original order in JavaScript
- Get last N elements from given list in Python
- Longest consecutive path from a given starting character
- JavaScript - summing numbers from strings nested in array
- Taking part from array of numbers by percent JavaScript
- Finding even length numbers from an array in JavaScript
- Get range of months from array based on another array JavaScript
- Get the smallest array from an array of arrays in JavaScript
- Get unique item from two different array in JavaScript
- Get Random value from a range of numbers in JavaScript?

Advertisements