forked from bittu1040/JavaScript-Coding-and-Notes
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathArray-Polyfills.js
More file actions
25 lines (17 loc) · 765 Bytes
/
Array-Polyfills.js
File metadata and controls
25 lines (17 loc) · 765 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
// Array polyfills
// push, pop, shift, foreach, split, find
// 1. Implement a polyfill for the Array.prototype.push() and Array.prototype.pop() method in JavaScript.
// The Array.prototype.push() method adds one or more elements to the end of an array and returns the new length of the array.
// The Array.prototype.pop() method removes the last element from an array and returns that element.
// This operation changes the length of the array.
Array.prototype.myPush= function(number){
this[this.length]=number;
return this.length;
}
console.log([1,2,3,4,5].myPush(80)); // 6
Array.prototype.myPop= function(){
let lastEle= this[this.length-1];
this.length=this.length-1;
return lastEle;
}
console.log([1,2,3,4,5].myPop()); // 5