Loading...
More JavaScript Posts
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; // 5list.head.value; // 3list.head.next.value; // 2list.tail.value; // 4[...list.map(e => e.value)]; // [3, 2, 1, 5, 4]list.removeAt(1); // 2list.getAt(1).value; // 1list.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 levenshteinDistance = (s, t) => {if (!s.length) return t.length;if (!t.length) return s.length;const arr = [];for (let i = 0; i <= t.length; i++) {arr[i] = [i];for (let j = 1; j <= s.length; j++) {arr[i][j] =i === 0? j: Math.min(arr[i - 1][j] + 1,arr[i][j - 1] + 1,arr[i - 1][j - 1] + (s[j - 1] === t[i - 1] ? 0 : 1));}}return arr[t.length][s.length];};levenshteinDistance('duck', 'dark'); // 2
const kMeans = (data, k = 1) => {const centroids = data.slice(0, k);const distances = Array.from({ length: data.length }, () =>Array.from({ length: k }, () => 0));const classes = Array.from({ length: data.length }, () => -1);let itr = true;while (itr) {itr = false;for (let d in data) {for (let c = 0; c < k; c++) {distances[d][c] = Math.hypot(...Object.keys(data[0]).map(key => data[d][key] - centroids[c][key]));}const m = distances[d].indexOf(Math.min(...distances[d]));if (classes[d] !== m) itr = true;classes[d] = m;}for (let c = 0; c < k; c++) {centroids[c] = Array.from({ length: data[0].length }, () => 0);const size = data.reduce((acc, _, d) => {if (classes[d] === c) {acc++;for (let i in data[0]) centroids[c][i] += data[d][i];}return acc;}, 0);for (let i in data[0]) {centroids[c][i] = parseFloat(Number(centroids[c][i] / size).toFixed(2));}}}return classes;};kMeans([[0, 0], [0, 1], [1, 3], [2, 0]], 2); // [0, 1, 1, 0]
//use this with canvas file finderfunction NewTab(testing) {window.open(testing, "_blank");}for(test in results) {var testing = 'https://unt.instructure.com/files/' + test + '/download';NewTab(testing);}
class Graph {constructor(directed = true) {this.directed = directed;this.nodes = [];this.edges = new Map();}addNode(key, value = key) {this.nodes.push({ key, value });}addEdge(a, b, weight) {this.edges.set(JSON.stringify([a, b]), { a, b, weight });if (!this.directed)this.edges.set(JSON.stringify([b, a]), { a: b, b: a, weight });}removeNode(key) {this.nodes = this.nodes.filter(n => n.key !== key);[...this.edges.values()].forEach(({ a, b }) => {if (a === key || b === key) this.edges.delete(JSON.stringify([a, b]));});}removeEdge(a, b) {this.edges.delete(JSON.stringify([a, b]));if (!this.directed) this.edges.delete(JSON.stringify([b, a]));}findNode(key) {return this.nodes.find(x => x.key === key);}hasEdge(a, b) {return this.edges.has(JSON.stringify([a, b]));}setEdgeWeight(a, b, weight) {this.edges.set(JSON.stringify([a, b]), { a, b, weight });if (!this.directed)this.edges.set(JSON.stringify([b, a]), { a: b, b: a, weight });}getEdgeWeight(a, b) {return this.edges.get(JSON.stringify([a, b])).weight;}adjacent(key) {return [...this.edges.values()].reduce((acc, { a, b }) => {if (a === key) acc.push(b);return acc;}, []);}indegree(key) {return [...this.edges.values()].reduce((acc, { a, b }) => {if (b === key) acc++;return acc;}, 0);}outdegree(key) {return [...this.edges.values()].reduce((acc, { a, b }) => {if (a === key) acc++;return acc;}, 0);}}const g = new Graph();g.addNode('a');g.addNode('b');g.addNode('c');g.addNode('d');g.addEdge('a', 'c');g.addEdge('b', 'c');g.addEdge('c', 'b');g.addEdge('d', 'a');g.nodes.map(x => x.value); // ['a', 'b', 'c', 'd'][...g.edges.values()].map(({ a, b }) => `${a} => ${b}`);// ['a => c', 'b => c', 'c => b', 'd => a']g.adjacent('c'); // ['b']g.indegree('c'); // 2g.outdegree('c'); // 1g.hasEdge('d', 'a'); // trueg.hasEdge('a', 'd'); // falseg.removeEdge('c', 'b');[...g.edges.values()].map(({ a, b }) => `${a} => ${b}`);// ['a => c', 'b => c', 'd => a']g.removeNode('c');g.nodes.map(x => x.value); // ['a', 'b', 'd'][...g.edges.values()].map(({ a, b }) => `${a} => ${b}`);// ['d => a']g.setEdgeWeight('d', 'a', 5);g.getEdgeWeight('d', 'a'); // 5
const nums = [1, 2, 3];const strs = Array.from('est');nums.push(6);nums.push(4, 9);strs.unshift('t');nums.length; // 6nums[nums.length - 1]; // 9strs[0]; // 't'strs[2]; // 's'nums.slice(1, 3); // [2, 3]nums.map(n => n * 2); // [2, 4, 6, 12, 8, 18]nums.filter(n => n % 2 === 0); // [2, 6, 4]nums.reduce((a, n) => a + n, 0); // 25strs.reverse(); // ['t', 's', 'e', 't']strs.join(''); // 'test'const nums = new Set([1, 2, 3]);nums.add(4);nums.add(1);nums.add(5);nums.add(4);nums.size; // 5nums.has(4); // truenums.delete(4);nums.has(4); // false[...nums]; // [1, 2, 3, 5]nums.clear();nums.size; // 0const items = new Map([[1, { name: 'John' }],[2, { name: 'Mary' }]]);items.set(4, { name: 'Alan' });items.set(2, { name: 'Jeff' });items.size; // 3items.has(4); // trueitems.get(2); // { name: 'Jeff' }items.delete(2);items.size; // 2[...items.keys()]; // [1, 4][...items.values()]; // [{ name: 'John' }, { name: 'Alan' }]items.clear();items.size; // 0