Last active
October 15, 2020 16:09
-
-
Save iankit3/903dc76e6a6413628e4d477b03e8eb90 to your computer and use it in GitHub Desktop.
Implementation of Array.prototype.splice JS Bin // source https://jsbin.com/butujubuqe
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<!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"> | |
Array.prototype.mySplice = function (start, count, ...elems) { | |
let first = this.slice(0, start), | |
middle = elems, | |
last = this.slice(start + count); | |
let arr = [...first, ...middle, ...last]; | |
this.length = arr.length; | |
for (let i = 0; i < this.length; ++i) { | |
this[i] = arr[i]; | |
} | |
}; | |
(function main() { | |
var x, y; | |
function reset() { | |
x = [1, 2, 3, 4, 5]; | |
y = [...x]; | |
} | |
let tests = [ | |
[1, 2], | |
[1, 4], | |
[1, 4, 12], | |
[1, 0, 10, 12], | |
[1, 2, 10, 12], | |
[1, 4, 10, 12], | |
[1, 15, 10, 12], | |
]; | |
function logAndAssert(x, y) { | |
console.log(x, y); | |
console.assert(JSON.stringify(x) === JSON.stringify(y)); | |
} | |
tests.forEach((test) => { | |
reset(); | |
console.log(`\n Running - Array.splice(${[...test]})`); | |
logAndAssert(x, y); | |
x.splice(...test); | |
y.mySplice(...test); | |
console.log("processed"); | |
logAndAssert(x, y); | |
}); | |
})(); | |
</script> | |
<script id="jsbin-source-javascript" type="text/javascript">Array.prototype.mySplice = function (start, count, ...elems) { | |
Array.prototype.mySplice = function (start, count, ...elems) { | |
let first = this.slice(0, start), | |
middle = elems, | |
last = this.slice(start + count); | |
let arr = [...first, ...middle, ...last]; | |
this.length = arr.length; | |
for (let i = 0; i < this.length; ++i) { | |
this[i] = arr[i]; | |
} | |
}; | |
(function main() { | |
var x, y; | |
function reset() { | |
x = [1, 2, 3, 4, 5]; | |
y = [...x]; | |
} | |
let tests = [ | |
[1, 2], | |
[1, 4], | |
[1, 4, 12], | |
[1, 0, 10, 12], | |
[1, 2, 10, 12], | |
]; | |
function logAndAssert(x, y) { | |
console.log(x, y); | |
console.assert(JSON.stringify(x) === JSON.stringify(y)); | |
} | |
tests.forEach((test) => { | |
reset(); | |
console.log(`\n Running - Array.splice(${[...test]})`); | |
logAndAssert(x, y); | |
x.splice(...test); | |
y.mySplice(...test); | |
console.log("processed"); | |
logAndAssert(x, y); | |
}); | |
})(); | |
</script></body> | |
</html> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Array.prototype.mySplice = function (start, count, ...elems) { | |
var end = start + count; | |
/*Remove elems b/w start and end | |
* Three steps | |
* 1. Mark them as null | |
* 2. Move them at the end | |
* 3. change the length of the array (removes the nulls if any) | |
*/ | |
// 1. | |
for (let i = start; i < end; ++i) { | |
this[i] = null; | |
} | |
// 2. | |
let len = this.length; | |
for (let i = 0; i < len; ++i) { | |
if (this[i] == null) { | |
this[i] = this[end]; | |
end++; | |
} | |
} | |
// 3. | |
if (this[this.length - 1] == null) { | |
this.length -= count; | |
} | |
/*Add each elem from elems*/ | |
if (elems.length > 0) { | |
let first = this.slice(0, start), | |
middle = elems, | |
last = this.slice(start + count); | |
let arr = [...first, ...middle, ...last]; | |
this.length = arr.length; | |
for (let i = 0; i < this.length; ++i) { | |
this[i] = arr[i]; | |
} | |
} | |
}; | |
(function main() { | |
var x, y; | |
function reset() { | |
x = [1, 2, 3, 4, 5]; | |
y = [...x]; | |
} | |
let tests = [ | |
[1, 2], | |
[1, 4], | |
[1, 4, 12], | |
[1, 0, 10, 12], | |
[1, 2, 10, 12], | |
[1, 4, 10, 12], | |
[1, 15, 10, 12], | |
]; | |
function logAndAssert(x, y) { | |
console.log(x, y); | |
console.assert(JSON.stringify(x) === JSON.stringify(y)); | |
} | |
tests.forEach((test) => { | |
reset(); | |
console.log(`\n Running - Array.splice(${[...test]})`); | |
logAndAssert(x, y); | |
x.splice(...test); | |
y.mySplice(...test); | |
console.log("processed"); | |
logAndAssert(x, y); | |
}); | |
})(); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment