Write a JavaScript function that removes an element from an array at a specified index. The function must validate inputs and handle errors appropriately.
Input: array = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11] , position = 10
Output: [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]
Input: array = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10] , position = -4
Output: Throw error => Position must be a valid integer within the array bounds
function removeElementUsingSplice(arr, position) {
if (!Array.isArray(arr)) {
throw new TypeError('First argument must be an array');
}
if (!Number.isInteger(position) || position < 0 || position >= arr.length) {
throw new RangeError('Position must be a valid integer within the array bounds');
}
arr.splice(position, 1);
return arr;
}
const array1 = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11];
const position1 = 10;
removeElementUsingSplice(array1,position1); //output: [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]
const array2 = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10];
const position2 = -4;
removeElementUsingSplice(array2,position2); //Throw error => Position must be a valid integer within the array bounds
A hash map (or dictionary) is a great way to solve this problem eff…
function removeElementUsingSlice(arr, position) {
if (!Array.isArray(arr)) {
throw new TypeError('First argument must be an array');
}
if (!Number.isInteger(position) || position < 0 || position >= arr.length) {
throw new RangeError('Position must be a valid integer within the array bounds');
}
return arr.slice(0, position).concat(arr.slice(position + 1));
}
const array1 = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11];
const position1 = 10;
removeElementUsingSlice(array1,position1); //output: [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]
const array2 = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10];
const position2 = -4;
removeElementUsingSlice(array2,position2); //Throw error => Position must be a valid integer within the array bounds
function removeElementUsingFilter(arr, position) {
if (!Array.isArray(arr)) {
throw new TypeError('First argument must be an array');
}
if (!Number.isInteger(position) || position < 0 || position >= arr.length) {
throw new RangeError('Position must be a valid integer within the array bounds');
}
return arr.filter((_, index) => index !== position);
}
const array1 = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11];
const position1 = 10;
removeElementUsingFilter(array1,position1); //output: [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]
const array2 = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10];
const position2 = -4;
removeElementUsingFilter(array2,position2); //Throw error => Position must be a valid integer within the array bounds
function removeElementUsingReduce(arr, position) {
if (!Array.isArray(arr)) {
throw new TypeError('First argument must be an array');
}
if (!Number.isInteger(position) || position < 0 || position >= arr.length) {
throw new RangeError('Position must be a valid integer within the array bounds');
}
return arr.reduce((acc, curr, index) => {
if (index !== position) acc.push(curr);
return acc;
}, []);
}
const array1 = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11];
const position1 = 10;
removeElementUsingReduce(array1,position1); //output: [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]
const array2 = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10];
const position2 = -4;
removeElementUsingReduce(array2,position2); //Throw error => Position must be a valid integer within the array bounds
function removeElementUsingForLoop(arr, position) {
if (!Array.isArray(arr)) {
throw new TypeError('First argument must be an array');
}
if (!Number.isInteger(position) || position < 0 || position >= arr.length) {
throw new RangeError('Position must be a valid integer within the array bounds');
}
let result = [];
for (let i = 0; i < arr.length; i++) {
if (i !== position) {
result.push(arr[i]);
}
}
return result;
}
const array1 = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11];
const position1 = 10;
removeElementUsingForLoop(array1,position1); //output: [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]
const array2 = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10];
const position2 = -4;
removeElementUsingForLoop(array2,position2); //Throw error => Position must be a valid integer within the array bounds
function removeElementUsingMap(arr, position) {
if (!Array.isArray(arr)) {
throw new TypeError('First argument must be an array');
}
if (!Number.isInteger(position) || position < 0 || position >= arr.length) {
throw new RangeError('Position must be a valid integer within the array bounds');
}
return arr.map((item, index) => (index === position ? undefined : item))
.filter(item => item !== undefined);
}
const array1 = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11];
const position1 = 10;
removeElementUsingMap(array1,position1); //output: [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]
const array2 = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10];
const position2 = -4;
removeElementUsingMap(array2,position2); //Throw error => Position must be a valid integer within the array bounds
function removeElementUsingCopyWithin(arr, position) {
if (!Array.isArray(arr)) {
throw new TypeError('First argument must be an array');
}
if (!Number.isInteger(position) || position < 0 || position >= arr.length) {
throw new RangeError('Position must be a valid integer within the array bounds');
}
arr.copyWithin(position, position + 1);
arr.length--; // reduce array length by 1
return arr;
}
const array1 = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11];
const position1 = 10;
removeElementUsingCopyWithin(array1,position1); //output: [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]
const array2 = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10];
const position2 = -4;
removeElementUsingCopyWithin(array2,position2); //Throw error => Position must be a valid integer within the array bounds
function removeElementUsingFill(arr, position) {
if (!Array.isArray(arr)) {
throw new TypeError('First argument must be an array');
}
if (!Number.isInteger(position) || position < 0 || position >= arr.length) {
throw new RangeError('Position must be a valid integer within the array bounds');
}
arr.fill(undefined, position, position + 1);
return arr.filter(item => item !== undefined);
}
const array1 = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11];
const position1 = 10;
removeElementUsingFill(array1,position1); //output: [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]
const array2 = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10];
const position2 = -4;
removeElementUsingFill(array2,position2); //Throw error => Position must be a valid integer within the array bounds
function removeElementUsingFindIndex(arr, position) {
if (!Array.isArray(arr)) {
throw new TypeError('First argument must be an array');
}
if (!Number.isInteger(position) || position < 0 || position >= arr.length) {
throw new RangeError('Position must be a valid integer within the array bounds');
}
let index = arr.findIndex((_, i) => i === position);
if (index !== -1) {
arr.splice(index, 1);
}
return arr;
}
const array1 = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11];
const position1 = 10;
removeElementUsingFindIndex(array1,position1); //output: [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]
const array2 = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10];
const position2 = -4;
removeElementUsingFindIndex(array2,position2); //Throw error => Position must be a valid integer within the array bounds
function removeElementUsingArrayFrom(arr, position) {
if (!Array.isArray(arr)) {
throw new TypeError('First argument must be an array');
}
if (!Number.isInteger(position) || position < 0 || position >= arr.length) {
throw new RangeError('Position must be a valid integer within the array bounds');
}
return Array.from(arr).filter((_, index) => index !== position);
}
const array1 = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11];
const position1 = 10;
removeElementUsingArrayFrom(array1,position1); //output: [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]
const array2 = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10];
const position2 = -4;
removeElementUsingArrayFrom(array2,position2); //Throw error => Position must be a valid integer within the array bounds