0% found this document useful (0 votes)
45 views

Codechum 15

The document discusses adding, removing, and modifying elements in JavaScript arrays. It provides examples of pushing elements to the end of an array, unshifting elements to the beginning, splicing elements from the end, and splicing elements from the beginning or a specific index.

Uploaded by

arvsimisimi
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
45 views

Codechum 15

The document discusses adding, removing, and modifying elements in JavaScript arrays. It provides examples of pushing elements to the end of an array, unshifting elements to the beginning, splicing elements from the end, and splicing elements from the beginning or a specific index.

Uploaded by

arvsimisimi
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
You are on page 1/ 2

//Add Elements to the End of an Array

var numbers = [];


var numElements = parseInt(prompt("Enter the number of elements: "));

for (var i = 0; i < numElements; i++) {


var element = parseFloat(prompt("Enter element " + (i + 1) + ": "));
numbers.push(element);
}

console.log(numbers);

//Add Elements to the Beginning of an Array

var colors = [];


var numElements = parseInt(prompt("Enter the number of elements: "));

for (var i = 0; i < numElements; i++) {


var element = prompt("Enter element " + (i + 1) + ": ");
colors.unshift(element);
}

console.log("Colors: [ " + colors.map(item => `'${item}'`).join(", ") + " ]");

//Remove the Last Element from an Array (3pts only)

var fruits = [];

var numElements = parseInt(prompt("Enter the number of elements to add: "));

for (var i = 0; i < numElements; i++) {


var element = prompt("Enter element " + (i + 1) + ": ");
fruits.push(element);
}

var numToRemove = parseInt(prompt("Enter the number of elements to remove: "));

fruits.splice(-numToRemove);

console.log("Modified Fruits: [ " + fruits.map(item => `'${item}'`).join(", ") +


" ]");

//Remove the Last Element from an Array (2pts only)

var fruits = [];

var numElements = parseInt(prompt("Enter the number of elements to add: "));

for (var i = 0; i < numElements; i++) {


var element = prompt("Enter element " + (i + 1) + ": ");
fruits.push(element);
}
var numToRemove = parseInt(prompt("Enter the number of elements to remove: "));

fruits.splice(-numToRemove);

console.log("Modified Fruits: [ " + fruits.map(item => `'${item}'`).join(",") +


" ]");

//Remove the First Element from an Array

var numbers = [];

var numElements = parseInt(prompt("Enter the number of elements: "));

for (var i = 0; i < numElements; i++) {


var element = prompt("Enter element " + (i + 1) + ": ");
numbers.push(element);
}

var numToRemove = parseInt(prompt("Enter the number of elements to remove: "));

numbers.splice(0, numToRemove);

console.log("Modified Numbers: [ " + numbers.map(item => `${item}`).join(", ") +


" ]");

//Add Elements at a Specific Index in an Array

You might also like