Skip to main content

Check if array is empty

Nov 19, 2022CodeCatch
Loading...

More JavaScript Posts

next cpu

Nov 18, 2022AustinLeath

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 &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>
)
}

binomial coefficient

Nov 19, 2022CodeCatch

0 likes • 0 views

const binomialCoefficient = (n, k) => {
if (Number.isNaN(n) || Number.isNaN(k)) return NaN;
if (k < 0 || k > n) return 0;
if (k === 0 || k === n) return 1;
if (k === 1 || k === n - 1) return n;
if (n - k < k) k = n - k;
let res = n;
for (let j = 2; j <= k; j++) res *= (n - j + 1) / j;
return Math.round(res);
};
binomialCoefficient(8, 2); // 28

doubly linked list

Nov 19, 2022CodeCatch

1 like • 0 views

class DoublyLinkedList {
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, previous: previousNode };
if (previousNode) previousNode.next = node;
if (nextNode) nextNode.previous = 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] || null;
const nextNode = this.nodes[index + 1] || null;
if (previousNode) previousNode.next = nextNode;
if (nextNode) nextNode.previous = previousNode;
return this.nodes.splice(index, 1);
}
clear() {
this.nodes = [];
}
reverse() {
this.nodes = this.nodes.reduce((acc, { value }) => {
const nextNode = acc[0] || null;
const node = { value, next: nextNode, previous: null };
if (nextNode) nextNode.previous = node;
return [node, ...acc];
}, []);
}
*[Symbol.iterator]() {
yield* this.nodes;
}
}
const list = new DoublyLinkedList();
list.insertFirst(1);
list.insertFirst(2);
list.insertFirst(3);
list.insertLast(4);
list.insertAt(3, 5);
list.size; // 5
list.head.value; // 3
list.head.next.value; // 2
list.tail.value; // 4
list.tail.previous.value; // 5
[...list.map(e => e.value)]; // [3, 2, 1, 5, 4]
list.removeAt(1); // 2
list.getAt(1).value; // 1
list.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

stripe api json iteration

Nov 18, 2022AustinLeath

0 likes • 1 view

var arr = [{
"success": true,
"data": [
{
"id": "ipi_1KrrbvDOiB2klwsKhuqUWqt1",
"object": "issuing.transaction",
"amount": -2743,
"amount_details": {
"atm_fee": null
},
"authorization": "iauth_1KrrbuDOiB2klwsKoFjQZhhd",
"balance_transaction": "txn_1KrrbwDOiB2klwsK1YkjJJRi",
"card": "ic_1Krqe5DOiB2klwsK44a35eiE",
"cardholder": "ich_1KrqL8DOiB2klwsKtBnZhzYr",
"created": 1650753567,
"currency": "usd",
"dispute": null,
"livemode": false,
"merchant_amount": -2743,
"merchant_currency": "usd",
"merchant_data": {
"category": "advertising_services",
"category_code": "7311",
"city": "San Francisco",
"country": "US",
"name": "Aeros Marketing, LLC",
"network_id": "1234567890",
"postal_code": "94103",
"state": "CA"
},
"metadata": {},
"type": "capture",
"wallet": null
},
{
"id": "ipi_1Krrbvc62B2klwsKhuqUWqt1",
"object": "issuing.transaction",
"amount": -9999,
"amount_details": {
"atm_fee": null
},
"authorization": "iauth_1KrrbuDOiB2klwsKoFjQZhhd",
"balance_transaction": "txn_1KrrbwDOiB2klwsK1YkjJJRi",
"card": "ic_1Krqe5DOiB2klwsK44a35eiE",
"cardholder": "ich_1KrqL8DOiB2klwsKtBnZhzYr",
"created": 1650753567,
"currency": "USD",
"dispute": null,
"livemode": false,
"merchant_amount": -9999,
"merchant_currency": "usd",
"merchant_data": {
"category": "fast_food",
"category_code": "7311",
"city": "San Francisco",
"country": "US",
"name": "Aeros Marketing, LLC",
"network_id": "1234567890",
"postal_code": "94103",
"state": "CA"
},
"metadata": {},
"type": "capture",
"wallet": null
}
]
}];
const reduced = arr.reduce((prev, curr) => {
prev.push({
amount: curr.data[0].merchant_amount,
id: curr.data[0].id,
currency: curr.data[0].currency,
category: curr.data[0].merchant_data.category
});
console.log(prev)
return prev;
}, []);

URL Search Term Generator

Jan 17, 2021CHS

0 likes • 0 views

const getSearchTerm = delimiter => {
let searchTerm = "";
for (let i = 1; i < commands.length - 1; i++)
searchTerm = searchTerm + commands[i] + delimiter;
searchTerm += commands[commands.length - 1];
return searchTerm;
};

heap sort

Nov 19, 2022CodeCatch

0 likes • 0 views

const heapsort = arr => {
const a = [...arr];
let l = a.length;
const heapify = (a, i) => {
const left = 2 * i + 1;
const right = 2 * i + 2;
let max = i;
if (left < l && a[left] > a[max]) max = left;
if (right < l && a[right] > a[max]) max = right;
if (max !== i) {
[a[max], a[i]] = [a[i], a[max]];
heapify(a, max);
}
};
for (let i = Math.floor(l / 2); i >= 0; i -= 1) heapify(a, i);
for (i = a.length - 1; i > 0; i--) {
[a[0], a[i]] = [a[i], a[0]];
l--;
heapify(a, 0);
}
return a;
};
heapsort([6, 3, 4, 1]); // [1, 3, 4, 6]