JavaScript: Remove specified elements from the right of a given array of elements
JavaScript fundamental (ES6 Syntax): Exercise-19 with Solution
Write a JavaScript program to remove specified elements from the right of a given array of elements.
Sample Solution:
JavaScript Code:
//#Source https://bit.ly/2neWfJ2
function remove_from_right(arr, n = -1){
return arr.slice(n);
}
console.log(remove_from_right([1, 2, 3]));
console.log(remove_from_right([1, 2, 3], -1));
console.log(remove_from_right([1, 2, 3], -2));
console.log(remove_from_right([1, 2, 3], -4));
Sample Output:
[3] [3] [2,3] [1,2,3]
Pictorial Presentation:
Flowchart:

Live Demo:
See the Pen javascript-basic-exercise-1-19 by w3resource (@w3resource) on CodePen.
Improve this sample solution and post your code through Disqus
Previous: Write a JavaScript program to remove specified elements from the left of a given array of elements.
Next: Write a JavaScript program to extend a 3-digit color code to a 6-digit color code.
What is the difficulty level of this exercise?
Test your Programming skills with w3resource's quiz.
- Weekly Trends
- Python Interview Questions and Answers: Comprehensive Guide
- Scala Exercises, Practice, Solution
- Kotlin Exercises practice with solution
- MongoDB Exercises, Practice, Solution
- SQL Exercises, Practice, Solution - JOINS
- Java Basic Programming Exercises
- SQL Subqueries
- Adventureworks Database Exercises
- C# Sharp Basic Exercises
- SQL COUNT() with distinct
- JavaScript String Exercises
- JavaScript HTML Form Validation
- Java Collection Exercises
- SQL COUNT() function
- SQL Inner Join