Js test

Run Settings
LanguageJavaScript
Language Version
Run Command
const factorialRecursive = (number) => { if (number > 1) { return number * factorialRecursive(number-1) } return 1 } const factorialLoop = (number) => { let factorial = 1 for(let i = 1; i<=number; i++) { factorial *= i } return factorial } // console.log(factorialRecursive(5)) // console.log(factorialLoop(5)) const fibonacciRecursive = (n, fib = [0,1]) => { const next = fib[0] + fib[1] if (n === next) return fib if (n < next) return 'Not Found' fib.shift() // remove first item fib.push(next) return fibonacciRecursive(n, fib) } const fibonacciLoop = (n) => { let fib = [0,1] let next = fib[0] + fib[1] if (n < next) return 'Not Found' while(n > next) { if (n === next) return fib fib.shift() // remove first item fib.push(next) next = fib[0] + fib[1] } return fib } // console.log(fibonacciRecursive(8)) // console.log(fibonacciLoop(8)) function reverseString(str) { const chars = str.split('') let reversed = [] let i = chars.length-1 while(i >= 0) { reversed.push(chars[i]) i-- } return reversed.join('') } console.log(reverseString("yoyo master")) function reverseStringRecursive(str) { } // reverseStringRecursive("yoyo master");
class Stack { constructor() { this.data = [] this.length = 0 } push(value) { this.data.push(value) this.length++ return this.data } peek() { return this.data[this.length-1] } pop() { this.data.pop() this.length-- return this.data } isEmpty() { return this.length < 1 } } const myStack = new Stack() myStack.push(2) myStack.push(5) console.log(myStack.data) console.log(myStack.peek()) myStack.pop() console.log(myStack.data) myStack.pop() console.log(myStack.isEmpty())
class Node { constructor(value) { this.value = value; this.next = null; } } class Queue { constructor() { this.first = null; this.last = null; this.length = 0; } peek() { return this.first } enqueue(value) { const newNode = new Node(value) if (this.length === 0) { this.first = newNode this.last = newNode } else { this.last.next = newNode this.last = newNode } this.length++ return this } dequeue() { if (this.length === 1) { this.first = null this.last = null } else { this.first = this.first.next } this.length-- return this } //isEmpty; } const myQueue = new Queue(); console.log(myQueue.peek()) myQueue.enqueue(5) myQueue.enqueue(7) console.log(myQueue) myQueue.dequeue() myQueue.dequeue() console.log(myQueue) //Joy //Matt //Pavel //Samir
class Node { constructor(value) { this.left = null; this.right = null; this.value = value; } } class BinarySearchTree { constructor() { this.root = null; } insert(value) { const newNode = new Node(value) if (!this.root) { this.root = newNode return this } let current = this.root while(true) { if(value < current.value) { if (!current.left) { current.left = newNode return this } current = current.left } else { if (!current.right) { current.right = newNode return this } current = current.right } } } lookup(value) { let current = this.root if(!current) return false while (current) { if (value < current.value) { current = current.left } if (value === current.value) { return current } else if (value > current.value) { current = current.right } } return false } remove(value) { let current = this.root let before = null let isLeft = false if(!current) return false while (current) { if (value < current.value) { before = current isLeft = true current = current.left } if (value === current.value) { break; } else if (value > current.value) { before = current isLeft = false current = current.right } } if(!current) return undefined // no child if (current.left === null && current.right == null) { isLeft ? before.left = null : before.right = null } else if (current.left && current.right) { // 2 child if (isLeft) { const succeessor = current.left before.left = succeessor succeessor.right = current.right } else { const succeessor = current.right before.right = succeessor succeessor.left = current.left } } else { // 1 child const child = current.left ? current.left : current.right isLeft ? before.left = child : before.right = child } return this } bfs() { let list = [] let queue = [this.root] while(queue.length > 0){ const current = queue.shift() list.push(current.value) if (current.left) queue.push(current.left) if (current.right) queue.push(current.right) } return list } dfs(node=this.root, list=[]) { // list.push(node.value) // preOrder if (node.left) this.dfs(node.left, list) // list.push(node.value) // inOrder if (node.right) this.dfs(node.right, list) list.push(node.value) // postOrder return list } } const tree = new BinarySearchTree(); tree.insert(9); tree.insert(4); tree.insert(6); tree.insert(20); tree.insert(15); tree.insert(1); tree.insert(170); // console.log(tree.bfs()) console.log(tree.dfs()) // console.log(tree.lookup(4)) // console.log(traverse(tree.root)); // tree.remove(20); // console.log(traverse(tree.root)); // tree.remove(170); // console.log(traverse(tree.root)); // console.log(tree.lookup(20)); // 9 // 4 20 //1 6 15 170 function traverse(node) { const tree = { value: node.value }; tree.left = node.left === null ? null : traverse(node.left); tree.right = node.right === null ? null : traverse(node.right); return tree; }
class Graph { constructor() { this.numberOfNodes = 0; this.adjacentList = { }; } addVertex(node) { this.adjacentList[node] = [] this.numberOfNodes++ } addEdge(node1, node2) { //undirected Graph if (!this.adjacentList[node1] || !this.adjacentList[node2]) return "No Vertex" this.adjacentList[node1].push(node2) this.adjacentList[node2].push(node1) } showConnections() { const allNodes = Object.keys(this.adjacentList); for (let node of allNodes) { let nodeConnections = this.adjacentList[node]; let connections = ""; let vertex; for (vertex of nodeConnections) { connections += vertex + " "; } console.log(node + "-->" + connections); } } } const myGraph = new Graph(); myGraph.addVertex('0'); myGraph.addVertex('1'); myGraph.addVertex('2'); myGraph.addVertex('3'); myGraph.addVertex('4'); myGraph.addVertex('5'); myGraph.addVertex('6'); myGraph.addEdge('3', '1'); myGraph.addEdge('3', '4'); myGraph.addEdge('4', '2'); myGraph.addEdge('4', '5'); myGraph.addEdge('1', '2'); myGraph.addEdge('1', '0'); myGraph.addEdge('0', '2'); myGraph.addEdge('6', '5'); myGraph.showConnections(); //Answer: // 0-->1 2 // 1-->3 2 0 // 2-->4 1 0 // 3-->1 4 // 4-->3 2 5 // 5-->4 6 // 6-->5
class Node { constructor(value) { this.value = value; this.next = null; } } class Stack { constructor() { this.top = null; this.bottom = null; this.length = 0; } peek() { console.log(this.top) } push(value) { const newNode = new Node(value) if (this.length < 1) { this.top = newNode this.bottom = newNode } else { newNode.next = this.top this.top = newNode } this.length++ } pop() { if (this.length === 1) { this.bottom = null this.top = null } else { this.top = this.top.next } this.length-- } isEmpty() { return this.length < 1 } } const myStack = new Stack(); myStack.push(2) myStack.push(5) // myStack.push(3) console.log(myStack) myStack.peek() myStack.pop() myStack.pop() console.log(myStack) // myStack.peek() // myStack.pop() console.log(myStack.isEmpty())
Editor Settings
Theme
Key bindings
Full width
Lines