Skip to main content

format duration

0 likes • Nov 19, 2022 • 0 views
JavaScript
Loading...

More JavaScript Posts

Redux Slice

CHS
0 likes • Mar 11, 2021 • 0 views
JavaScript
import { createSlice } from "@reduxjs/toolkit";
const alert = createSlice({
name: "alert",
initialState: {
msg: "",
status: "",
},
reducers: {
set_alert: (state, action) => {
return {
...state,
msg: action.payload.msg,
status: action.payload.status,
};
},
clear_alert: (state, action) => {
return {
...state,
msg: "",
status: "",
};
},
},
});
export default alert.reducer;
const { set_alert, clear_alert } = alert.actions;
export const setAlert = (msg, status) => dispatch => {
dispatch(set_alert({ msg, status }));
setTimeout(() => dispatch(clear_alert()), 4000);
};
export const clearAlert = () => dispatch => {
dispatch(clear_alert());
};

Composing components

0 likes • Nov 19, 2022 • 0 views
JavaScript
function Welcome(props) {
return <h1>Hello, {props.name}</h1>;
}
function App() {
return (
<div>
<Welcome name="Sara" />
<Welcome name="Cahal" />
<Welcome name="Edite" />
</div>
);
}
ReactDOM.render(
<App />,
document.getElementById('root')
);

Untitled

0 likes • Apr 14, 2023 • 8 views
JavaScript
const quickSort = arr => {
const a = [...arr];
if (a.length < 2) return a;
const pivotIndex = Math.floor(arr.length / 2);
const pivot = a[pivotIndex];
const [lo, hi] = a.reduce(
(acc, val, i) => {
if (val < pivot || (val === pivot && i != pivotIndex)) {
acc[0].push(val);
} else if (val > pivot) {
acc[1].push(val);
}
return acc;
},
[[], []]
);
return [...quickSort(lo), pivot, ...quickSort(hi)];
};
console.log(quickSort([1, 6, 1, 5, 3, 2, 1, 4])) // [1, 1, 1, 2, 3, 4, 5, 6]

next cpu

0 likes • Nov 18, 2022 • 1 view
JavaScript
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 &rarr;</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 &rarr;</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 &rarr;</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 &rarr;</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>
)
}

QM Helper

0 likes • Mar 22, 2022 • 1 view
JavaScript
//QM Helper by Leif Messinger
//Groups numbers by number of bits and shows their binary representations.
//To be used on x.com
const minterms = prompt("Enter your minterms separated by commas").split(",").map(x => parseInt(x.trim()));
const maxNumBits = minterms.reduce(function(a, b) {
return Math.max(a, Math.log2(b));
});
const bitGroups = [];
for(let i = 0; i < maxNumBits; ++i){
bitGroups.push([]);
}
for(const minterm of minterms){
let outputString = (minterm+" ");
//Count the bits
let count = 0;
for (var i = maxNumBits; i >= 0; --i) {
if((minterm >> i) & 1){
++count;
outputString += "1";
}else{
outputString += "0";
}
}
bitGroups[count].push(outputString);
}
document.body.textContent = "";
document.body.style.setProperty("white-space", "pre");
for(const group of bitGroups){
for(const outputString of group){
document.body.textContent += outputString + "\r\n";
}
}

Bitwise XOR operator

0 likes • Nov 19, 2022 • 0 views
JavaScript
//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}`);