trekhleb / trekhleb/javascript-algorithms

Code misbehavior(mild) : LinkedList.append(), according to code, should add to tail, but actually append to head

Open
#544 1 comment 0 reactions 0 assignees View on GitHub

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

  1. Copy the LinkedList.js, LinkedListNode.js, Comparator.js code to the console of Chrome DevTool
    • Add console.log to the append() method.
  2. 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

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. 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

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.