fix(curriculum): use object.assign to prevent overwriting prototypes if user uses class instead of function (#37648)

* fix: use object.assign to prevent overwriting when using class

* fix: remove non-used isBinarySearchTree method
This commit is contained in:
Randell Dawson
2019-12-13 13:04:05 -08:00
committed by Tom
parent 63b9be8faf
commit 85e7fe45d5
12 changed files with 353 additions and 395 deletions

View File

@ -64,36 +64,9 @@ function BinarySearchTree() {
<div id='js-teardown'>
```js
BinarySearchTree.prototype = {
isBinarySearchTree() {
if (this.root == null) {
return null;
} else {
var check = true;
function checkTree(node) {
if (node.left != null) {
var left = node.left;
if (left.value > node.value) {
check = false;
} else {
checkTree(left);
}
}
if (node.right != null) {
var right = node.right;
if (right.value < node.value) {
check = false;
} else {
checkTree(right);
}
}
}
checkTree(this.root);
return check;
}
}
};
BinarySearchTree.prototype = {
BinarySearchTree.prototype = Object.assign(
BinarySearchTree.prototype,
{
inOrder() {
if (!this.root) {
return null;
@ -107,7 +80,8 @@ BinarySearchTree.prototype = {
traverseInOrder(this.root);
return result;
}
};
}
);
```
</div>
@ -133,7 +107,6 @@ function BinarySearchTree() {
const searchTree = function(current) {
if (current.value > element) {
if (current.left) {
//si existe
return searchTree(current.left);
} else {
current.left = new Node(element);

View File

@ -61,7 +61,9 @@ function BinarySearchTree() {
<div id='js-teardown'>
```js
BinarySearchTree.prototype = {
BinarySearchTree.prototype = Object.assign(
BinarySearchTree.prototype,
{
add: function(value) {
var node = this.root;
if (node == null) {
@ -90,7 +92,8 @@ BinarySearchTree.prototype = {
return searchTree(node);
}
}
};
}
);
```
</div>

View File

@ -68,7 +68,11 @@ var DoublyLinkedList = function() {
<div id='js-teardown'>
```js
DoublyLinkedList.prototype = {
DoublyLinkedList.prototype = Object.assign(
DoublyLinkedList.prototype,
{
print() {
if (this.head == null) {
return null;
@ -97,7 +101,7 @@ DoublyLinkedList.prototype = {
return result;
};
}
};
});
```
</div>

View File

@ -68,7 +68,9 @@ function BinarySearchTree() {
<div id='js-teardown'>
```js
BinarySearchTree.prototype = {
BinarySearchTree.prototype = Object.assign(
BinarySearchTree.prototype,
{
add: function(value) {
var node = this.root;
if (node == null) {
@ -114,35 +116,9 @@ BinarySearchTree.prototype = {
traverseInOrder(this.root);
return result;
}
},
isBinarySearchTree() {
if (this.root == null) {
return null;
} else {
var check = true;
function checkTree(node) {
if (node.left != null) {
var left = node.left;
if (left.value > node.value) {
check = false;
} else {
checkTree(left);
}
}
if (node.right != null) {
var right = node.right;
if (right.value < node.value) {
check = false;
} else {
checkTree(right);
}
}
}
checkTree(this.root);
return check;
}
}
};
);
```
</div>

View File

@ -106,7 +106,9 @@ function BinarySearchTree() {
<div id='js-teardown'>
```js
BinarySearchTree.prototype = {
BinarySearchTree.prototype = Object.assign(
BinarySearchTree.prototype,
{
add: function(value) {
var node = this.root;
if (node == null) {
@ -152,35 +154,9 @@ BinarySearchTree.prototype = {
traverseInOrder(this.root);
return result;
}
},
isBinarySearchTree() {
if (this.root == null) {
return null;
} else {
var check = true;
function checkTree(node) {
if (node.left != null) {
var left = node.left;
if (left.value > node.value) {
check = false;
} else {
checkTree(left);
}
}
if (node.right != null) {
var right = node.right;
if (right.value < node.value) {
check = false;
} else {
checkTree(right);
}
}
}
checkTree(this.root);
return check;
}
}
};
);
```
</div>

View File

@ -125,7 +125,9 @@ function BinarySearchTree() {
<div id='js-teardown'>
```js
BinarySearchTree.prototype = {
BinarySearchTree.prototype = Object.assign(
BinarySearchTree.prototype,
{
add: function(value) {
var node = this.root;
if (node == null) {
@ -199,7 +201,8 @@ BinarySearchTree.prototype = {
return check;
}
}
};
}
);
```
</div>

View File

@ -66,13 +66,10 @@ function BinarySearchTree() {
<div id='js-teardown'>
```js
BinarySearchTree.prototype = {
BinarySearchTree.prototype = Object.assign(
BinarySearchTree.prototype,
{
add: function(value) {
var node = this.root;
if (node == null) {
this.root = new Node(value);
return;
} else {
function searchTree(node) {
if (value < node.value) {
if (node.left == null) {
@ -92,10 +89,17 @@ BinarySearchTree.prototype = {
return null;
}
}
var node = this.root;
if (node == null) {
this.root = new Node(value);
return;
} else {
return searchTree(node);
}
}
};
}
);
```
</div>

View File

@ -61,13 +61,10 @@ function BinarySearchTree() {
<div id='js-teardown'>
```js
BinarySearchTree.prototype = {
BinarySearchTree.prototype = Object.assign(
BinarySearchTree.prototype,
{
add: function(value) {
var node = this.root;
if (node == null) {
this.root = new Node(value);
return;
} else {
function searchTree(node) {
if (value < node.value) {
if (node.left == null) {
@ -87,10 +84,17 @@ BinarySearchTree.prototype = {
return null;
}
}
var node = this.root;
if (node == null) {
this.root = new Node(value);
return;
} else {
return searchTree(node);
}
}
};
}
);
```
</div>

View File

@ -58,13 +58,10 @@ function BinarySearchTree() {
<div id='js-teardown'>
```js
BinarySearchTree.prototype = {
BinarySearchTree.prototype = Object.assign(
BinarySearchTree.prototype,
{
add: function(value) {
var node = this.root;
if (node == null) {
this.root = new Node(value);
return;
} else {
function searchTree(node) {
if (value < node.value) {
if (node.left == null) {
@ -83,7 +80,13 @@ BinarySearchTree.prototype = {
} else {
return null;
};
};
}
var node = this.root;
if (node == null) {
this.root = new Node(value);
return;
} else {
return searchTree(node);
};
},
@ -105,7 +108,8 @@ BinarySearchTree.prototype = {
return result;
};
}
};
}
);
```
</div>

View File

@ -63,7 +63,9 @@ var DoublyLinkedList = function() {
<div id='js-teardown'>
```js
DoublyLinkedList.prototype = {
DoublyLinkedList.prototype = Object.assign(
DoublyLinkedList.prototype,
{
add(data) {
if (this.head == null) {
this.head = new Node(data, null);
@ -108,7 +110,8 @@ DoublyLinkedList.prototype = {
return result;
};
}
};
}
);
```
</div>

View File

@ -64,13 +64,10 @@ function BinarySearchTree() {
<div id='js-teardown'>
```js
BinarySearchTree.prototype = {
BinarySearchTree.prototype = Object.assign(
BinarySearchTree.prototype,
{
add: function(value) {
var node = this.root;
if (node == null) {
this.root = new Node(value);
return;
} else {
function searchTree(node) {
if (value < node.value) {
if (node.left == null) {
@ -90,10 +87,16 @@ BinarySearchTree.prototype = {
return null;
}
}
var node = this.root;
if (node == null) {
this.root = new Node(value);
return;
} else {
return searchTree(node);
}
}
};
}
);
```
</div>

View File

@ -76,13 +76,10 @@ function BinarySearchTree() {
<div id='js-teardown'>
```js
BinarySearchTree.prototype = {
BinarySearchTree.prototype = Object.assign(
BinarySearchTree.prototype,
{
add: function(value) {
var node = this.root;
if (node == null) {
this.root = new Node(value);
return;
} else {
function searchTree(node) {
if (value < node.value) {
if (node.left == null) {
@ -102,10 +99,18 @@ BinarySearchTree.prototype = {
return null;
}
}
var node = this.root;
if (node == null) {
this.root = new Node(value);
return;
} else {
return searchTree(node);
}
}
};
}
);
```
</div>