fix(curriculum) - added new tests to prevent invalid solutions in the Remove Elements from a Linked List by Index challenge (#36168)

* fix: added new tests to prevent invalid solutions

* fix: corrected tests and added solution

* fix: added linked word

Co-Authored-By: Manish Giri <manish.giri.me@gmail.com>

* fix: added by one

Co-Authored-By: Manish Giri <manish.giri.me@gmail.com>

* fix: removed duplicate code

* fix: changed verbiage

Co-Authored-By: Manish Giri <manish.giri.me@gmail.com>

* fix: added extra line to avoid linting errors
This commit is contained in:
Randell Dawson 2019-06-20 10:34:08 -07:00 committed by Manish Giri
parent a13d116aab
commit a6afaf1eb6

View File

@ -15,8 +15,7 @@ Just like our <code>remove(element)</code> method, which <a href="learn/coding-i
## Instructions
<section id='instructions'>
Write a <code>removeAt(index)</code> method that removes and returns a node at a given <code>index</code>. The method should return <code>null</code> if the given <code>index</code> is either negative, or greater than or equal to the <code>length</code> of the linked list.
Note
Remember to keep count of the <code>currentIndex</code>.
<strong>Note:</strong> Remember to keep count of the <code>currentIndex</code>.
</section>
## Tests
@ -25,15 +24,19 @@ Remember to keep count of the <code>currentIndex</code>.
```yml
tests:
- text: Your <code>LinkedList</code> class should have a <code>removeAt</code> method.
testString: assert((function(){var test = new LinkedList(); return (typeof test.removeAt === 'function')}()), 'Your <code>LinkedList</code> class should have a <code>removeAt</code> method.');
- text: Your <code>removeAt</code> method should reduce the <code>length</code> of the linked list
testString: assert((function(){var test = new LinkedList(); test.add('cat'); test.add('dog'); test.add('kitten'); test.removeAt(1); return test.size() === 2}()), 'Your <code>removeAt</code> method should reduce the <code>length</code> of the linked list');
- text: Your <code>removeAt</code> method should also return the element of the removed node.
testString: assert((function(){var test = new LinkedList(); test.add('cat'); test.add('dog'); test.add('kitten'); return test.removeAt(1) === 'dog'}()), 'Your <code>removeAt</code> method should also return the element of the removed node.');
- text: Your <code>removeAt</code> method should also return <code>null</code> if the given index is less than <code>0</code>
testString: assert((function(){var test = new LinkedList(); test.add('cat'); test.add('dog'); test.add('kitten'); return (test.removeAt(-1) === null)}()), 'Your <code>removeAt</code> method should also return <code>null</code> if the given index is less than <code>0</code>');
- text: Your <code>removeAt</code> method should also return <code>null</code> if the given index is equal or more than the <code>length</code> of the linked list.
testString: assert((function(){var test = new LinkedList(); test.add('cat'); test.add('dog'); test.add('kitten'); return (test.removeAt(3) === null)}()), 'Your <code>removeAt</code> method should also return <code>null</code> if the given index is equal or more than the <code>length</code> of the linked list.');
testString: assert((function(){var test = new LinkedList(); return (typeof test.removeAt === 'function')}()));
- text: Your <code>removeAt</code> method should reduce the <code>length</code> of the linked list by one.
testString: assert((function(){var test = new LinkedList(); test.add('cat'); test.add('dog'); test.add('kitten'); test.removeAt(1); return test.size() === 2}()));
- text: Your <code>removeAt</code> method should remove the element at the specified index from the linked list.
testString: assert((function(){var test = new LinkedList(); test.add('cat'); test.add('dog'); test.add('kitten'); test.add('bird');test.removeAt(1); return JSON.stringify(test.head()) === '{"element":"cat","next":{"element":"kitten","next":{"element":"bird","next":null}}}'}()));
- text: When only one element is present in the linked list, your <code>removeAt</code> method should remove and return the element at specified index, and reduce the length of the linked list.
testString: assert((function(){var test = new LinkedList(); test.add('cat'); var removedItem = test.removeAt(0); return test.head() === null && test.size() === 0 && removedItem === 'cat';}()));
- text: Your <code>removeAt</code> method should return the element of the removed node.
testString: assert((function(){var test = new LinkedList(); test.add('cat'); test.add('dog'); test.add('kitten'); return test.removeAt(1) === 'dog'}()));
- text: Your <code>removeAt</code> method should return <code>null</code> and the linked list should not change if the given index is less than <code>0</code>.
testString: assert((function(){var test = new LinkedList(); test.add('cat'); test.add('dog'); test.add('kitten'); var removedItem = test.removeAt(-1); return removedItem === null && JSON.stringify(test.head()) === '{"element":"cat","next":{"element":"dog","next":{"element":"kitten","next":null}}}'}()));
- text: Your <code>removeAt</code> method should return <code>null</code> and the linked list should not change if the given index is greater than or equal to the <code>length</code> of the list.
testString: assert((function(){var test = new LinkedList(); test.add('cat'); test.add('dog'); test.add('kitten'); var removedItem = test.removeAt(3); return removedItem === null && JSON.stringify(test.head()) === '{"element":"cat","next":{"element":"dog","next":{"element":"kitten","next":null}}}'}()));
```
@ -85,14 +88,13 @@ function LinkedList() {
```
</div>
</section>
## Solution
<section id='solution'>
```js
function LinkedList() {
var length = 0;
var head = null;
@ -137,8 +139,6 @@ function LinkedList() {
if (index === 0) {
var removed = head.element;
head = currentNode.next;
length--;
return removed;
} else {
while (count < index) {
previous = currentNode;
@ -147,12 +147,11 @@ function LinkedList() {
}
var removed = previous.next.element;
previous.next = currentNode.next;
length--;
return removed;
}
length--;
return removed;
};
}
```
</section>