• Nov 18, 2022 •AustinLeath
0 likes • 0 views
// Get the text that the user has selected const getSelectedText = () => window.getSelection().toString(); getSelectedText();
• Nov 19, 2022 •CodeCatch
0 likes • 1 view
// `arr` is an array const clone = arr => arr.slice(0); // Or const clone = arr => [...arr]; // Or const clone = arr => Array.from(arr); // Or const clone = arr => arr.map(x => x); // Or const clone = arr => JSON.parse(JSON.stringify(arr)); // Or const clone = arr => arr.concat([]);
class LinkedList { constructor() { this.nodes = []; } get size() { return this.nodes.length; } get head() { return this.size ? this.nodes[0] : null; } get tail() { return this.size ? this.nodes[this.size - 1] : null; } insertAt(index, value) { const previousNode = this.nodes[index - 1] || null; const nextNode = this.nodes[index] || null; const node = { value, next: nextNode }; if (previousNode) previousNode.next = node; this.nodes.splice(index, 0, node); } insertFirst(value) { this.insertAt(0, value); } insertLast(value) { this.insertAt(this.size, value); } getAt(index) { return this.nodes[index]; } removeAt(index) { const previousNode = this.nodes[index - 1]; const nextNode = this.nodes[index + 1] || null; if (previousNode) previousNode.next = nextNode; return this.nodes.splice(index, 1); } clear() { this.nodes = []; } reverse() { this.nodes = this.nodes.reduce( (acc, { value }) => [{ value, next: acc[0] || null }, ...acc], [] ); } *[Symbol.iterator]() { yield* this.nodes; } } const list = new LinkedList(); list.insertFirst(1); list.insertFirst(2); list.insertFirst(3); list.insertLast(4); list.insertAt(3, 5); list.size; // 5 list.head.value; // 3 list.head.next.value; // 2 list.tail.value; // 4 [...list.map(e => e.value)]; // [3, 2, 1, 5, 4] list.removeAt(1); // 2 list.getAt(1).value; // 1 list.head.next.value; // 1 [...list.map(e => e.value)]; // [3, 1, 5, 4] list.reverse(); [...list.map(e => e.value)]; // [4, 5, 1, 3] list.clear(); list.size; // 0
const getTimezone = () => Intl.DateTimeFormat().resolvedOptions().timeZone; // Example getTimezone();
• Jan 26, 2023 •AustinLeath
0 likes • 5 views
function printHeap(heap, index, level) { if (index >= heap.length) { return; } console.log(" ".repeat(level) + heap[index]); printHeap(heap, 2 * index + 1, level + 1); printHeap(heap, 2 * index + 2, level + 1); } //You can call this function by passing in the heap array and the index of the root node, which is typically 0, and level = 0. let heap = [3, 8, 7, 15, 17, 30, 35, 2, 4, 5, 9]; printHeap(heap,0,0)
• Sep 13, 2023 •C S
0 likes • 2 views
/** * @param {number[]} nums * @return {boolean} */ var containsDuplicate = function(nums) { return new Set(nums).size !== nums.length };