Skip to main content

URL Search Term Generator

CHS
0 likes • Jan 17, 2021 • 0 views
JavaScript
Loading...

More JavaScript Posts

Temperature Converter

0 likes • Nov 19, 2022 • 0 views
JavaScript
const celsiusToFahrenheit = (celsius) => celsius * 9/5 + 32;
const fahrenheitToCelsius = (fahrenheit) => (fahrenheit - 32) * 5/9;
// Examples
celsiusToFahrenheit(15); // 59
celsiusToFahrenheit(0); // 32
celsiusToFahrenheit(-20); // -4
fahrenheitToCelsius(59); // 15
fahrenheitToCelsius(32); // 0

Responsive Bootstrap Card.Img

CHS
0 likes • Jan 25, 2023 • 3 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>
);
}

localStorage Cookie Consent

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

Date Time Testing

0 likes • Nov 18, 2022 • 1 view
JavaScript
var d = new Date();
var d = new Date(milliseconds);
var d = new Date(dateString);
var d = new Date(year, month, day, hours, minutes, seconds, milliseconds);

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]]

getURLParameters

0 likes • Nov 19, 2022 • 0 views
JavaScript
const getURLParameters = url =>
(url.match(/([^?=&]+)(=([^&]*))/g) || []).reduce(
(a, v) => (
(a[v.slice(0, v.indexOf('='))] = v.slice(v.indexOf('=') + 1)), a
),
{}
);