• Jan 25, 2023 •C S
1 like • 17 views
export const Main = () => { const [title, setTitle] = useState(''); return ( <ChildComponent title={title} onChange={e => setTitle(e.target.value)} /> ); }; export const ChildComponent = ({ title, onChange }) => { return ( <Box component="form" onSubmit={() => {}} noValidate mt={5}> <TextField fullWidth id="title" label="Title" name="title" autoComplete="title" autoFocus color="primary" variant="filled" InputLabelProps={{ shrink: true }} value={title} onChange={onChange} /> </Box> ); };
• Nov 19, 2022 •CodeCatch
0 likes • 1 view
const getURLParameters = url => (url.match(/([^?=&]+)(=([^&]*))/g) || []).reduce( (a, v) => ( (a[v.slice(0, v.indexOf('='))] = v.slice(v.indexOf('=') + 1)), a ), {} );
0 likes • 0 views
const compactObject = val => { const data = Array.isArray(val) ? val.filter(Boolean) : val; return Object.keys(data).reduce( (acc, key) => { const value = data[key]; if (Boolean(value)) acc[key] = typeof value === 'object' ? compactObject(value) : value; return acc; }, Array.isArray(val) ? [] : {} ); }; const obj = { a: null, b: false, c: true, d: 0, e: 1, f: '', g: 'a', h: [null, false, '', true, 1, 'a'], i: { j: 0, k: false, l: 'a' } }; compactObject(obj); // { c: true, e: 1, g: 'a', h: [ true, 1, 'a' ], i: { l: 'a' } }
• Jan 26, 2023 •AustinLeath
0 likes • 5 views
function printHeap(heap, index, level) { if (index >= heap.length) { return; } console.log(" ".repeat(level) + heap[index]); printHeap(heap, 2 * index + 1, level + 1); printHeap(heap, 2 * index + 2, level + 1); } //You can call this function by passing in the heap array and the index of the root node, which is typically 0, and level = 0. let heap = [3, 8, 7, 15, 17, 30, 35, 2, 4, 5, 9]; printHeap(heap,0,0)
// There are n rings and each ring is either red, green, or blue. The rings are distributed across ten rods labeled from 0 to 9. // You are given a string rings of length 2n that describes the n rings that are placed onto the rods. Every two characters in rings forms a color-position pair that is used to describe each ring where: // The first character of the ith pair denotes the ith ring's color ('R', 'G', 'B'). // The second character of the ith pair denotes the rod that the ith ring is placed on ('0' to '9'). // For example, "R3G2B1" describes n == 3 rings: a red ring placed onto the rod labeled 3, a green ring placed onto the rod labeled 2, and a blue ring placed onto the rod labeled 1. // Return the number of rods that have all three colors of rings on them. let rings = "B0B6G0R6R0R6G9"; var countPoints = function(rings) { let sum = 0; // Always 10 Rods for (let i = 0; i < 10; i++) { if (rings.includes(`B${i}`) && rings.includes(`G${i}`) && rings.includes(`R${i}`)) { sum+=1; } } return sum; }; console.log(countPoints(rings));
0 likes • 3 views
const deepMerge = (a, b, fn) => [...new Set([...Object.keys(a), ...Object.keys(b)])].reduce( (acc, key) => ({ ...acc, [key]: fn(key, a[key], b[key]) }), {} ); deepMerge( { a: true, b: { c: [1, 2, 3] } }, { a: false, b: { d: [1, 2, 3] } }, (key, a, b) => (key === 'a' ? a && b : Object.assign({}, a, b)) ); // { a: false, b: { c: [ 1, 2, 3 ], d: [ 1, 2, 3 ] } }