Skip to main content

next cpu

0 likes • Nov 18, 2022 • 1 view
JavaScript
Loading...

More JavaScript Posts

geometric progression

0 likes • Nov 19, 2022 • 0 views
JavaScript
const geometricProgression = (end, start = 1, step = 2) =>
Array.from({
length: Math.floor(Math.log(end / start) / Math.log(step)) + 1,
}).map((_, i) => start * step ** i);
geometricProgression(256); // [1, 2, 4, 8, 16, 32, 64, 128, 256]
geometricProgression(256, 3); // [3, 6, 12, 24, 48, 96, 192]
geometricProgression(256, 1, 4); // [1, 4, 16, 64, 256]

Untitled

CAS
0 likes • Aug 13, 2023 • 2 views
JavaScript
function sortNumbers(arr) {
return arr.slice().sort((a, b) => a - b);
}
const unsortedArray = [4, 1, 9, 6, 3, 5];
const sortedArray = sortNumbers(unsortedArray);
console.log("Unsorted Numbers:", unsortedArray);
console.log("\n");
console.log("Sorted Numbers:", sortedArray);

Generate a random HEX color

0 likes • Nov 18, 2022 • 0 views
JavaScript
// Generate a random HEX color
randomColor = () => `#${Math.random().toString(16).slice(2, 8).padStart(6, '0')}`;
// Or
const randomColor = () => `#${(~~(Math.random()*(1<<24))).toString(16)}`;

Ticking clock

0 likes • Nov 19, 2022 • 0 views
JavaScript
function Clock(props) {
return (
<div>
<h1>Hello, world!</h1>
<h2>It is {props.date.toLocaleTimeString()}.</h2>
</div>
);
}
function tick() {
ReactDOM.render(
<Clock date={new Date()} />,
document.getElementById('root')
);
}
setInterval(tick, 1000);

Timer

0 likes • Feb 5, 2021 • 2 views
JavaScript
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();
}
}

localStorage Cookie Consent

0 likes • Oct 9, 2023 • 122 views
JavaScript
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>
)
}