• Nov 19, 2022 •CodeCatch
0 likes • 3 views
const euclideanDistance = (a, b) => Math.hypot(...Object.keys(a).map(k => b[k] - a[k])); euclideanDistance([1, 1], [2, 3]); // ~2.2361 euclideanDistance([1, 1, 1], [2, 3, 2]); // ~2.4495
• Sep 13, 2023 •C S
0 likes • 2 views
/** * @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] } } } };
• Oct 9, 2023 •Helper
0 likes • 290 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> ) }
const longestPalindrome = (s) => { if(s.length === 1) return s const odd = longestOddPalindrome(s) const even = longestEvenPalindrome(s) if(odd.length >= even.length) return odd if(even.length > odd.length) return even }; const longestOddPalindrome = (s) => { let longest = 0 let longestSubStr = "" for(let i = 1; i < s.length; i++) { let currentLongest = 1 let currentLongestSubStr = "" let left = i - 1 let right = i + 1 while(left >= 0 && right < s.length) { const leftLetter = s[left] const rightLetter = s[right] left-- right++ if(leftLetter === rightLetter) { currentLongest += 2 currentLongestSubStr = s.slice(left+1, right) } else break } if(currentLongest > longest) { if(currentLongestSubStr) longestSubStr = currentLongestSubStr else longestSubStr = s[i] longest = currentLongest } } return longestSubStr } const longestEvenPalindrome = (s) => { let longest = 0 let longestSubStr = "" for(let i = 0; i < s.length - 1; i++) { if(s[i] !== s[i+1]) continue; let currentLongest = 2 let currentLongestSubStr = "" let left = i - 1 let right = i + 2 while(left >= 0 && right < s.length) { const leftLetter = s[left] const rightLetter = s[right] left-- right++ if(leftLetter === rightLetter) { currentLongest += 2 currentLongestSubStr = s.slice(left+1, right) } else break } if(currentLongest > longest) { if(currentLongestSubStr) longestSubStr = currentLongestSubStr else longestSubStr = s[i] + s[i+1] longest = currentLongest } } return longestSubStr } console.log(longestPalindrome("babad"))
0 likes • 0 views
function Clock(props) { return ( <div> <h1>Hello, world!</h1> <h2>It is {props.date.toLocaleTimeString()}.</h2> </div> ); } function tick() { ReactDOM.render( <Clock date={new Date()} />, document.getElementById('root') ); } setInterval(tick, 1000);
• Oct 15, 2022 •CodeCatch
3 likes • 4 views
const MongoClient = require('mongodb').MongoClient; const assert = require('assert'); // Connection URL const url = 'mongodb://localhost:27017'; // Database Name const dbName = 'myproject'; // Use connect method to connect to the server MongoClient.connect(url, function(err, client) { assert.equal(null, err); console.log("Connected successfully to server"); const db = client.db(dbName); client.close(); });