fix(learn): Added more robust logic for the remove test case in Create A Hash Table (#41283)

* Added extra test cases for hashtable.remove

* Minor Change

* Adjusted tests to use user created add function / Modified description

* Made test case description much more clear

* Changed instructions to be more clear

* Minor change to instructions

Co-authored-by: Randell Dawson <5313213+RandellDawson@users.noreply.github.com>

Co-authored-by: Randell Dawson <5313213+RandellDawson@users.noreply.github.com>
This commit is contained in:
Caden Parker
2021-03-09 10:45:20 -08:00
committed by GitHub
parent 5063b1104e
commit 256e7f416d

View File

@ -20,6 +20,8 @@ Let's create the basic functionality of a hash table. We've created a naive hash
Be sure to write your code to account for collisions!
**Note:** The `remove` method tests won't pass until the `add` and `lookup` methods are correctly implemented.
# --hints--
The HashTable data structure should exist.
@ -50,20 +52,6 @@ assert(
);
```
The HashTable should have a remove method.
```js
assert(
(function () {
var test = false;
if (typeof HashTable !== 'undefined') {
test = new HashTable();
}
return typeof test.remove === 'function';
})()
);
```
The HashTable should have a lookup method.
```js
@ -78,6 +66,20 @@ assert(
);
```
The HashTable should have a remove method.
```js
assert(
(function () {
var test = false;
if (typeof HashTable !== 'undefined') {
test = new HashTable();
}
return typeof test.remove === 'function';
})()
);
```
The add method should add key value pairs and the lookup method should return the values associated with a given key.
```js
@ -103,14 +105,40 @@ assert(
if (typeof HashTable !== 'undefined') {
test = new HashTable();
}
test.add = addMethodSolution;
test.add('key', 'value');
test.remove('key');
return !test.collection.hasOwnProperty(hashValue);
})()
);
```
The remove method should only remove the correct key value pair.
```js
assert(
(function () {
var test = false;
var hashValue = hash('key');
if (typeof HashTable !== 'undefined') {
test = new HashTable();
}
test.add('key', 'value');
test.add('yek', 'value');
test.add('altKey', 'value');
test.remove('yek');
if (test.lookup('yek') || !test.lookup('key') || !test.lookup('altKey')) {
return false;
}
test.remove('key');
return !test.collection.hasOwnProperty(hashValue) && test.lookup('altKey');
})()
);
```
Items should be added using the hash function.
```js
@ -165,14 +193,6 @@ var hash = string => {
}
return hash;
};
var addMethodSolution = function(key, val) {
var theHash = hash(key);
if (!this.collection.hasOwnProperty(theHash)) {
this.collection[theHash] = {};
}
this.collection[theHash][key] = val;
}
```
## --seed-contents--