• Nov 19, 2022 •CodeCatch
0 likes • 0 views
const copyToClipboard = str => { const el = document.createElement('textarea'); el.value = str; el.setAttribute('readonly', ''); el.style.position = 'absolute'; el.style.left = '-9999px'; document.body.appendChild(el); const selected = document.getSelection().rangeCount > 0 ? document.getSelection().getRangeAt(0) : false; el.select(); document.execCommand('copy'); document.body.removeChild(el); if (selected) { document.getSelection().removeAllRanges(); document.getSelection().addRange(selected); } }; copyToClipboard('Lorem ipsum'); // 'Lorem ipsum' copied to clipboard.
• Nov 18, 2022 •AustinLeath
0 likes • 2 views
import Head from 'next/head' import Image from 'next/image' import styles from '../styles/Home.module.css' const { exec } = require("child_process"); exec("sensors|grep "high"|grep "Core"|cut -d "+" -f2|cut -d "." -f1|sort -nr|sed -n 1p", (error, stdout, stderr) => { if (error) { console.log(`error: ${error.message}`); return; } if (stderr) { console.log(`stderr: ${stderr}`); return; } console.log(`stdout: ${stdout}`); }); export default function Home() { return ( <div className={styles.container}> <Head> <title>Create Next App</title> <meta name="description" content="Generated by create next app" /> <link rel="icon" href="/favicon.ico" /> </Head> <main className={styles.main}> <h1 className={styles.title}> Welcome to <a href="https://nextjs.org">Next.js!</a> </h1> <p className={styles.description}> Get started by editing{' '} <code className={styles.code}>pages/index.js</code> </p> <div className={styles.grid}> <a href="https://nextjs.org/docs" className={styles.card}> <h2>Documentation →</h2> <p>Find in-depth information about Next.js features and API.</p> </a> <a href="https://nextjs.org/learn" className={styles.card}> <h2>Learn →</h2> <p>Learn about Next.js in an interactive course with quizzes!</p> </a> <a href="https://github.com/vercel/next.js/tree/canary/examples" className={styles.card} > <h2>Examples →</h2> <p>Discover and deploy boilerplate example Next.js projects.</p> </a> <a href="https://vercel.com/new?utm_source=create-next-app&utm_medium=default-template&utm_campaign=create-next-app" className={styles.card} > <h2>Deploy →</h2> <p> Instantly deploy your Next.js site to a public URL with Vercel. </p> </a> </div> </main> <footer className={styles.footer}> <a href="https://vercel.com?utm_source=create-next-app&utm_medium=default-template&utm_campaign=create-next-app" target="_blank" rel="noopener noreferrer" > Powered by{' '} <span className={styles.logo}> <Image src="/vercel.svg" alt="Vercel Logo" width={72} height={16} /> </span> </a> </footer> </div> ) }
• Feb 5, 2021 •LeifMessinger
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(); } }
//JavaScript program to swap two variables //take input from the users let a = prompt('Enter the first variable: '); let b = prompt('Enter the second variable: '); //using destructuring assignment [a, b] = [b, a]; console.log(`The value of a after swapping: ${a}`); console.log(`The value of b after swapping: ${b}`);
0 likes • 1 view
//JavaScript program to swap two variables //take input from the users let a = prompt('Enter the first variable: '); let b = prompt('Enter the second variable: '); // XOR operator a = a ^ b b = a ^ b a = a ^ b console.log(`The value of a after swapping: ${a}`); console.log(`The value of b after swapping: ${b}`);
// Or const randomColor = () => `#${(~~(Math.random()*(1<<24))).toString(16)}`; console.log(randomColor);