Skip to content

Conversation

@debrayc
Copy link

@debrayc debrayc commented Apr 7, 2017

No description provided.

Copy link

@rae-ralston rae-ralston left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🤓 🙌



it('exists', () => {

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

be consistant in spacing


it('returns head node', () => {
const linkedList = new LinkedList()
linkedList.insert('apple')

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

you used the before hook... that should do this before each test... why are you still doing it for every test?

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

it didn't work... lol... need to take that out...

linkedList.insert('banana')

expect(linkedList.getTailNode().data).to.equal('banana')

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

spacing

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Everywhere, I'm not going to comment them all. It almost doesn't matter which way you pick, just be maximum consistent before submitting PR

}

getHeadNode(){
if (!this) {

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

change this to a ternary
!this ? null : this.head

}

getTailNode(){
if (!this) {

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

ternary


contains(string){
let current = this.head
if (this.head.data == string) {

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If I have 10 things in my list will this still be right? seems like it's checking the first two levels....


insert(data){
const current = new Node(data)
this.size ++

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

maybe increment size at the end in case something fails?

this.head = newHead
}
isEmpty() {
if (this.head === null && this.tail === null && this.size === 0) {

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

returning true/false is a code smell. You should always look at how you can refactor.
if you just do return (this.head === null && this.size === 0) that will return t/f

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I did that but in one of my tests, I found that one of the nodes still existed in the tail of my linked list somehow?? Weird as hell because I thought that if you removed the head node without reassigning 'this.head' that the whole list would magically disappear...

}

getNext() {
if (this.next) {

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

ternary

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You're the best @rachel-ftw... Thanks for your detailed feedback, the ninja training is strong in this one... lol

let linkedList = new LinkedList()
linkedList.insert('apple')
linkedList.insert('banana')
})
Copy link

@jason00111 jason00111 Apr 7, 2017

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It seems like you want this function to be run before each test. You could use beforeEach for this (before only runs once). See https://mochajs.org/#hooks.

Also, the way linkedList is declared on line 8 means that this variable is only defined within the block on lines 7 through 11.

But all the tests need access to linkedList so this must be fixed.

One way to fix this is to declare linkedList on line 6 (global scope) without assigning it a value and then assign a value to linkedList within the beforeEach like you are already doing.

So, line 6 would become let linkedList and line 8 would be linkedList = new LinkedList().

Then you could remove these three lines in each of the tests,

const linkedList = new LinkedList()
linkedList.insert('apple')
linkedList.insert('banana')


expect(linkedList.tail).to.equal(null) &&
expect(linkedList.head).to.equal(null) &&
expect(linkedList.size).to.equal(0)

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The two &&'s are not necessary.


context('isEmpty', () => {
const linkedList = new LinkedList()
linkedList.isEmpty()

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This line (145) is not necessary


})
})
})

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Good job on these linked list tests! There are some blank lines which could be removed to make it look a little cleaner.

getHeadNode(){
if (!this) {
return null
}

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Lines 12 to 14 can be removed. If you put a console.log inside the if you'll find that this code is never run, even when there is no head node.

getTailNode(){
if (!this) {
return null
}

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Lines 19 to 21 can be removed. See above.

if (current.data !== string && current.next == null) {
return false
}
}

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This method/function called contains does not meet the spec because it only returns true if it finds the data in one of the first two nodes.

For example, if your test contained,

linkedList.insert('apple')
linkedList.insert('banana')
linkedList.insert('cranberry')

expect(linkedList.contains("apple")).to.equal(true)
expect(linkedList.contains("banana")).to.equal(true)
expect(linkedList.contains("cranberry")).to.equal(true)

the "apple" and "banana" tests will pass but the "cranberry" test will fail.

I suggest you start over from scratch with this function. I suggest using a whiteboard or paper or pseudocode to figure out an algorithm which will find out if a piece of data is in the linked list.


expect(linkedList.find("banana").data).to.equal('banana')

})

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It would be good to include a test for the -1 case as well.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants