• Jan 25, 2023 •C S
0 likes • 9 views
// Vanilla JS Solution: var app = document.getElementById('app'); var typewriter = new Typewriter(app, { loop: true }); typewriter .typeString("I'm John and I'm a super cool web developer") .pauseFor(3000) .deleteChars(13) // "web developer" = 13 characters .typeString("person to talk with!") // Will display "I'm John and I'm a super cool person to talk with!" .start(); // React Solution: import Typewriter from 'typewriter-effect'; <Typewriter options={{ loop: true }} onInit={typewriter => { typewriter .typeString("I'm John and I'm a super cool web developer") .pauseFor(3000) .deleteChars(13) // "web developer" = 13 characters .typeString("person to talk with!") // Will display "I'm John and I'm a super cool person to talk with!" .start(); }} />
• Nov 19, 2022 •CodeCatch
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]
• 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)
• Oct 15, 2022 •CodeCatch
2 likes • 9 views
var invertTree = function(root) { const reverseNode = node => { if (node == null) { return null } reverseNode(node.left); reverseNode(node.right); let holdLeft = node.left; node.left = node.right; node.right = holdLeft; return node; } return reverseNode(root); };
0 likes • 1 view
//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}`);
• Oct 9, 2023 •Helper
0 likes • 286 views
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> ) }