Link to the lesson
Instructions
In step 3 the instructions read:
Add an
if
statement in thewhile
loop that does a strict equality check of the current node’s.key
andkey
. If the two keys are the same, overwrite the current node’skey
andvalue
properties with the key-value pair we want to store.
Right after that it reads:
Outside of the last
if
statement, write a condition that checks if the current node is the tail node. Check whatNode
methods are available to you in the Node.js file.
This is how I implemented that and it passed:
if(current.data.key === key){
current.data = {key, value}
}
if(!current.getNextNode()){
current.setNextNode(new Node({key, value}))
break;
}
Question
I wonder why the instructions only ask us to break the loop if the second condition is met (step 5). Why should we continue the while loop if we’ve already overwritten a value?
Result
I tested my code that passed all tests. The linked list seems to be polluted with old values:
Adjustment:
If I break out of the loop if the first condition is met as well, the result seems to make more sense to me:
Did I miss a feature of Hashmaps or should the instructions and the test for passing be extended?