blog.devbitbyte.comHow to write JavaScript unshift and shift array methods from scratchunshift Add an element to the front of an array Array.prototype._unshift = function(...values) { const newArray = [...values, ...this] this.length = 0 this.push(...newArray) return this.length; }; const fruits = ['apple', 'mango', 'banana']...Dec 9, 2023·1 min read
blog.devbitbyte.comHow to write JavaScript array methods push and pop from scratchpush Add an element to the end of the array(JS has a dynamic array). Array.prototype._push = function(...values) { for(let i = 0; i < values.length; i++) { this[this.length] = values[i] } return this.length } const fruits = ['ap...Dec 9, 2023·1 min read
blog.devbitbyte.comHow to write JavaScript array methods includes and join from scratchincludes Return true if the item is in the array otherwise, return false. Array.prototype._includes = function(value, fromIndex = 0) { for(let i = fromIndex; i < this.length; i++) { if(this[i] === value) { return true ...Dec 8, 2023·1 min read
blog.devbitbyte.comHow to write JavaScript findIndex and findLastIndex from scratchfindIndex Returns the index of the element that returns true on a callback function. Array.prototype._findIndex = function(cbFunction) { for(let i = 0; i < this.length; i++) { if(cbFunction(this[i], i, this)) { return i ...Dec 8, 2023·1 min read
blog.devbitbyte.comHow to write JavaScript find and findLast from scratchfind Returns the first element that satisfies the testing function otherwise returns undefined. Array.prototype._find = function(cbFunction) { for(let i = 0; i < this.length; i++) { if(cbFunction(this[i], i, this)) { return th...Dec 7, 2023·1 min read