Skip to main content

Reddit Regex Comment Upvoter

0 likes • May 17, 2021 • 2 views
JavaScript
Loading...

More JavaScript Posts

localStorage Cookie Consent

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

binary search

0 likes • Nov 19, 2022 • 1 view
JavaScript
const binarySearch = (arr, item) => {
let l = 0,
r = arr.length - 1;
while (l <= r) {
const mid = Math.floor((l + r) / 2);
const guess = arr[mid];
if (guess === item) return mid;
if (guess > item) r = mid - 1;
else l = mid + 1;
}
return -1;
};
binarySearch([1, 2, 3, 4, 5], 1); // 0
binarySearch([1, 2, 3, 4, 5], 5); // 4
binarySearch([1, 2, 3, 4, 5], 6); // -1

power set

0 likes • Nov 19, 2022 • 1 view
JavaScript
const powerset = arr =>
arr.reduce((a, v) => a.concat(a.map(r => r.concat(v))), [[]]);
powerset([1, 2]); // [[], [1], [2], [1, 2]]

bucket sort

0 likes • Nov 19, 2022 • 0 views
JavaScript
const bucketSort = (arr, size = 5) => {
const min = Math.min(...arr);
const max = Math.max(...arr);
const buckets = Array.from(
{ length: Math.floor((max - min) / size) + 1 },
() => []
);
arr.forEach(val => {
buckets[Math.floor((val - min) / size)].push(val);
});
return buckets.reduce((acc, b) => [...acc, ...b.sort((a, b) => a - b)], []);
};
bucketSort([6, 3, 4, 1]); // [1, 3, 4, 6]

Responsive Bootstrap Card.Img

CHS
0 likes • Jan 25, 2023 • 2 views
JavaScript
const ResponsiveCardImg = styled(Card.Img)`
// For screens wider than 768px
@media(min-width: 768px) {
// Your responsive styles here.
}
// For screens smaller than 768px
@media(max-width: 768px) {
// Your responsive styles here.
}
`
export default function App() {
return (
<MainContainer>
<Container>
<Card text='white' bg='dark' style={styles.mainCard}>
<ResponsiveCardImg
style={styles.cardImage}
onClick={handleClick}
src={apeImage}
alt='Degenerate Ape Academy'
/>
<Card.Body>
<Card.Title className='text-center' as='h2'>
{apeId}
</Card.Title>
</Card.Body>
</Card>
</Container>
</MainContainer>
);
}

LeetCode Two Sum

CHS
0 likes • Sep 13, 2023 • 2 views
JavaScript
/**
* @param {number[]} nums
* @param {number} target
* @return {number[]}
*/
var twoSum = function(nums, target) {
for(let i = 0; i < nums.length; i++) {
for(let j = 0; j < nums.length; j++) {
if(nums[i] + nums[j] === target && i !== j) {
return [i, j]
}
}
}
};