Skip to content

Instantly share code, notes, and snippets.

@loiane loiane/index.html
Created Sep 9, 2017

Embed
What would you like to do?
JS Bin // source http://jsbin.com/wuyopaz
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width">
<title>JS Bin</title>
</head>
<body>
<script id="jsbin-javascript">
// @ts-check
function printArray(myArray) {
for (var i = 0; i < myArray.length; i++) {
console.log(myArray[i]);
}
}
var numbers = [0, 1, 2, 3, 4, 5, 6, 7, 8, 9];
// add a new element to the numbers array
numbers[numbers.length] = 10;
numbers.push(11);
numbers.push(12, 13);
printArray(numbers);
// insert first position manually
for (var i = numbers.length; i >= 0; i--) {
numbers[i] = numbers[i - 1];
}
numbers[0] = -1;
printArray(numbers);
// using method unshift
numbers.unshift(-2);
printArray(numbers);
numbers.unshift(-4, -3);
printArray(numbers);
// **** Removing elements
numbers.pop();
// remove first position manually
/* for (var i=0; i<numbers.length; i++){
numbers[i] = numbers[i+1];
}*/
printArray(numbers);
console.log(numbers.length);
// using method shift
numbers.shift();
printArray(numbers);
console.log(numbers.length);
//* *** Removing and Adding elements from the middle of the array or specific position
// splice method - parameter (index, howManyPositionsToBeRemoved, item1...itemX)
numbers.splice(5, 3);
console.log('----');
printArray(numbers);
numbers.splice(5, 0, 2, 3, 4);
console.log('----');
printArray(numbers);
console.log('----');
numbers.splice(5, 3, 2, 3, 4);
printArray(numbers);
</script>
<script id="jsbin-source-javascript" type="text/javascript">// @ts-check
function printArray(myArray) {
for (var i = 0; i < myArray.length; i++) {
console.log(myArray[i]);
}
}
var numbers = [0, 1, 2, 3, 4, 5, 6, 7, 8, 9];
// add a new element to the numbers array
numbers[numbers.length] = 10;
numbers.push(11);
numbers.push(12, 13);
printArray(numbers);
// insert first position manually
for (var i = numbers.length; i >= 0; i--) {
numbers[i] = numbers[i - 1];
}
numbers[0] = -1;
printArray(numbers);
// using method unshift
numbers.unshift(-2);
printArray(numbers);
numbers.unshift(-4, -3);
printArray(numbers);
// **** Removing elements
numbers.pop();
// remove first position manually
/* for (var i=0; i<numbers.length; i++){
numbers[i] = numbers[i+1];
}*/
printArray(numbers);
console.log(numbers.length);
// using method shift
numbers.shift();
printArray(numbers);
console.log(numbers.length);
//* *** Removing and Adding elements from the middle of the array or specific position
// splice method - parameter (index, howManyPositionsToBeRemoved, item1...itemX)
numbers.splice(5, 3);
console.log('----');
printArray(numbers);
numbers.splice(5, 0, 2, 3, 4);
console.log('----');
printArray(numbers);
console.log('----');
numbers.splice(5, 3, 2, 3, 4);
printArray(numbers);</script></body>
</html>
// @ts-check
function printArray(myArray) {
for (var i = 0; i < myArray.length; i++) {
console.log(myArray[i]);
}
}
var numbers = [0, 1, 2, 3, 4, 5, 6, 7, 8, 9];
// add a new element to the numbers array
numbers[numbers.length] = 10;
numbers.push(11);
numbers.push(12, 13);
printArray(numbers);
// insert first position manually
for (var i = numbers.length; i >= 0; i--) {
numbers[i] = numbers[i - 1];
}
numbers[0] = -1;
printArray(numbers);
// using method unshift
numbers.unshift(-2);
printArray(numbers);
numbers.unshift(-4, -3);
printArray(numbers);
// **** Removing elements
numbers.pop();
// remove first position manually
/* for (var i=0; i<numbers.length; i++){
numbers[i] = numbers[i+1];
}*/
printArray(numbers);
console.log(numbers.length);
// using method shift
numbers.shift();
printArray(numbers);
console.log(numbers.length);
//* *** Removing and Adding elements from the middle of the array or specific position
// splice method - parameter (index, howManyPositionsToBeRemoved, item1...itemX)
numbers.splice(5, 3);
console.log('----');
printArray(numbers);
numbers.splice(5, 0, 2, 3, 4);
console.log('----');
printArray(numbers);
console.log('----');
numbers.splice(5, 3, 2, 3, 4);
printArray(numbers);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.