Loading...
More JavaScript Posts
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]
const powerset = arr =>arr.reduce((a, v) => a.concat(a.map(r => r.concat(v))), [[]]);powerset([1, 2]); // [[], [1], [2], [1, 2]]
alert("bruh")
const getURLParameters = url =>(url.match(/([^?=&]+)(=([^&]*))/g) || []).reduce((a, v) => ((a[v.slice(0, v.indexOf('='))] = v.slice(v.indexOf('=') + 1)), a),{});
import React, { useState, useEffect } from 'react';import Link from 'next/link';export default function CookieBanner() {// Initialize with a default value (false if not previously set in localStorage)const [cookieConsent, setCookieConsent] = useState(() =>localStorage.getItem('cookieConsent') === 'true' ? true : false);useEffect(() => {const newCookieConsent = cookieConsent ? 'granted' : 'denied';window.gtag('consent', 'update', {analytics_storage: newCookieConsent});localStorage.setItem('cookieConsent', String(cookieConsent));}, [cookieConsent]);return !cookieConsent && (<div><div><p>We use cookies to enhance the user experience.{' '}<Link href='/privacy/'>View privacy policy</Link></p></div><div><button type="button" onClick={() => setCookieConsent(false)}>Decline</button><button type="button" onClick={() => setCookieConsent(true)}>Allow</button></div></div>)}
class Timer{start(){this.startTime = Date.now();}stop(){const dt = Date.now() - this.startTime;console.log(dt);return dt;}getExecTime(fun, asynchronous){return asynchronous ? this.getAsyncExecTime(fun) : this.getSyncExecTime(fun);}getSyncExecTime(fun){this.start();fun();return this.stop();}async getAsyncExecTime(fun){this.start();await fun();return this.stop();}static average(fun, numTimes, asynchronous = false){const times = [];const promises = [];let timer = new Timer();if(asynchronous){for(let i = 0; i < numTimes; i++){timer.getAsyncExecTime(fun);}}else{for(let i = 0; i < numTimes; i++){timer.getSyncExecTime(fun);}}return (times => times.reduce(((sum, acc) => sume + acc), 0) / times.length);}static async timeAsync(fun, numTimes){if(numTimes <= 1) return new Timer().getAsyncExecTime(fun);const promises = [];let timer = new Timer(true);for(let i = 0; i < numTimes; i++){promises.push(fun().catch((e)=>{}));}try{await Promise.all(promises).catch((e)=>{});}catch(e){//do absolutely nothing}return timer.stop();}constructor(start = false){if(start) this.start();}}