trekhleb / trekhleb/javascript-algorithms
Code misbehavior(mild) : LinkedList.append(), according to code, should add to tail, but actually append to head
Open
Nobody has claimed this yet.
- Dominant language
- JavaScript
- Stars
- 197k
- Forks
- 31k
- PR merge metrics
- No merged PRs in 30d
Description
How to recreate the problem
- Copy the LinkedList.js, LinkedListNode.js, Comparator.js code to the console of Chrome DevTool
- Add
console.logto theappend()method.
- Add
- Add test code
let a = new LinkedList();
a.append(5)
a.append(7)
// In the ```append``` method, it was ```this.tail = newNode```, but in chrome console,
// it can be observed that it was added to **head**.
// If I changed the ```this.tail = newNode``` to ```this.head = newNode```, it appended to the **tail**
// If I add some ```console.log``` in the ```append``` method,
// it could be observed that the class instance is already been changed
// before the ```console.log``` was reached(applies even when ```console.log``` is at the start of the ```method```.
Intention
- As a beginner, I just want to know why it happened, as far as I know about 'If it is not broke, don't fix it.', I am still interested in what actually happened in JavaScript.
- My instinct told me it maybe has something to do with mutability, but I am not sure what exactly it is.
Source
- Code
- Line 51 and Line 52
- Code
- Comparator Code
Contributor guide
First steps
- Read the whole issue, then the project's contributing guide.
- Comment on the issue to say you are picking it up — it saves two people doing the same work.
- Fork the repository and make your change on a branch.
- Open a pull request that references the issue number.
Research direction
Read src/data-structures/linked-list/LinkedList.js around lines 51–52, then inspect LinkedListNode.js and Comparator.js. Reproduce the Chrome DevTools example with two append calls and compare the observed head and tail references. Done means explaining the observed behavior and identifying whether the implementation or the reproduction is responsible.
Written by the indexing model from the issue text.
Assessment
- Tech stack
- javascript
- Domain
- backend
- Issue type
- Bug
- Difficulty
- 3/5
- Estimated time
- 1-2 days
- Activity status
- Stale
- Clarity
- Needs clarification
- Newbie friendliness
- 25/100