From 256e7f416dc40306f8fe8a2e5222f63f78a30397 Mon Sep 17 00:00:00 2001 From: Caden Parker <36315399+Ne0nWinds@users.noreply.github.com> Date: Tue, 9 Mar 2021 10:45:20 -0800 Subject: [PATCH] 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> --- .../data-structures/create-a-hash-table.md | 66 ++++++++++++------- 1 file changed, 43 insertions(+), 23 deletions(-) diff --git a/curriculum/challenges/english/10-coding-interview-prep/data-structures/create-a-hash-table.md b/curriculum/challenges/english/10-coding-interview-prep/data-structures/create-a-hash-table.md index 9bc2b5281f..05f5c099bb 100644 --- a/curriculum/challenges/english/10-coding-interview-prep/data-structures/create-a-hash-table.md +++ b/curriculum/challenges/english/10-coding-interview-prep/data-structures/create-a-hash-table.md @@ -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--