Javascript Notebook

for loop

for (const i of [...Array(n).keys()])

Object.keys and find

const j = +Object.keys(myArray).find(key => myArray[key] == someValue)

Map

const myMap = new Map()
myMap.set(0, 'zzz')
myMap.get(0) // 'zzz'
myMap.set(1, 'ooo')
for (const [key, value] of myMap) {
    console.log(`${key} = ${value}`)
}

Stack


In [53]:
const myStack = []
myStack.push(1)
myStack.pop()


Out[53]:
1

Queue


In [54]:
const myQ = []
myQ.push(1)
myQ.push(2)
myQ.shift()
myQ


Out[54]:
[ 2 ]

Linked List

Singly Linked List


In [41]:
const createSLL = () => {
    let head = null
    
    const getHead = () => head
    
    const push = value => {
        let node = {value, next: null}
        if (!head) {
            head = node
        }
        else {
            let current = head
            while (current.next) {
                current = current.next
            }
            current.next = node
        }
    }
    return {getHead, push}
}

In [42]:
var sll = createSLL()

In [43]:
sll.push(3);
sll.push(4);
sll.push(5);

In [44]:
sll.getHead()


Out[44]:
{ value: 3, next: { value: 4, next: { value: 5, next: null } } }

Doubly Linked List


In [45]:
var createDLL = () => {
    let head = null
    const getHead = () => head
    const push = value => {
        let current = head
        let previous = head
        if (!head) {
            head = {value, previous: null, next: null}
        }
        else {
            while (current && current.next) {
                previous = current
                current = current.next
            }
            current.next = {value, previous: current, next: null}
        }
    }
    return {getHead, push}
}

In [47]:
var dll = createDLL()
dll.push(2)
dll.push(3)
dll.push(4)
dll.push(5)

In [51]:
dll.getHead().next.previous


Out[51]:
{ value: 2,
  previous: null,
  next: 
   { value: 3,
     previous: [Circular],
     next: { value: 4, previous: [Circular], next: [Object] } } }

Singly Linked List Node


In [ ]:
var ListNode = val => {
    let value = val
    let next = null
    const getValue = () => val
    const setValue = newVal => {
        value = newVal
    }
    const getNext = () => next
    const setNext = nextNode => {
        next = nextNode
    }
    const appendEnd = endVal => {}
    return {getValue, setValue, getNext, setNext}
}

Tree


In [ ]:
var TreeNode = val => {
    let value = val
    let children = []
    const getValue = () => value
    const getChildren = () => children
    const setValue = newVal => {value = newVal}
    const addChild = childNode => {children.push(childNode)}
    return{getValue, getChildren, setValue, addChild}
}

Binary Tree


In [1]:
var TreeNode = val => {
    let value = val
    let left = null
    let right = null
    const getValue = () => value
    const setValue = newVal => {value = newVal}
    const getLeft = () => left
    const getRight = () => right
    const setLeft = Node => {left = Node}
    const setRight = Node => {right = Node}
    return{getValue, setValue, getLeft, getRight, setLeft, setRight}
}

In [5]:
var IOT = node => {
    if (node) {
        IOT(node.getLeft())
        console.log(node.getValue())
        IOT(node.getRight())
    }
}

In [6]:
var PreOT = node => {
    if (node) {
        console.log(node.getValue())
        PreOT(node.getLeft())
        PreOT(node.getRight())
    }
}

In [7]:
var PostOT = node => {
    if (node) {
        PostOT(node.getLeft())
        PostOT(node.getRight())
        console.log(node.getValue())
    }
}

In [11]:
var myNode = TreeNode(10)
myNode.setLeft(TreeNode(5))
myNode.getLeft().setLeft(TreeNode(3))
myNode.getLeft().setRight(TreeNode(7))
myNode.setRight(TreeNode(20))

In [12]:
IOT(myNode)


3
5
7
10
20

In [13]:
PreOT(myNode)


10
5
3
7
20

In [14]:
PostOT(myNode)


3
7
5
20
10

Graph

LeetCode

Adjacency Matrix

1. Two Sum


In [7]:
var twoSum = (nums, target) => {
    for (const i of [...Array(nums.length).keys()]) {
        if (nums.includes(target - nums[i])) {
            const j = +Object.keys(nums).find(key => nums[key] == target - nums[i] && key != i)
            if (j) {
                return [i, j]
            }
        }
    }
    return "not found"
}

2. Add Two Numbers


In [20]:
function ListNode(val) {
    this.val = val;
    this.next = null;
}

In [21]:
function appendAtEnd(Node, val) {
    if (!Node.next) {
        Node.next = ListNode(value)
    }
    else {
        appendAtEnd(Node.next, val)
    }
}

In [24]:
var list1 = ListNode(2)

In [26]:
list1

In [ ]:


In [ ]:


In [ ]:


In [ ]:


In [86]:
function foo() {
    let a = 2
    function bar() {
        console.log(a)
    }
    return bar
}

In [87]:
var baz = foo()

In [88]:
baz()


2

In [ ]:


In [93]:
var foo = () => {
    let  a = 2
    var bar = () => console.log(a)
    return {bar}
}

In [94]:
var baz = foo()

In [96]:
baz.bar()


2

In [ ]: