• Nov 19, 2022 •CodeCatch
0 likes • 0 views
const countSubstrings = (str, searchValue) => { let count = 0, i = 0; while (true) { const r = str.indexOf(searchValue, i); if (r !== -1) [count, i] = [count + 1, r + 1]; else return count; } }; countSubstrings('tiktok tok tok tik tok tik', 'tik'); // 3 countSubstrings('tutut tut tut', 'tut'); // 4
const binomialCoefficient = (n, k) => { if (Number.isNaN(n) || Number.isNaN(k)) return NaN; if (k < 0 || k > n) return 0; if (k === 0 || k === n) return 1; if (k === 1 || k === n - 1) return n; if (n - k < k) k = n - k; let res = n; for (let j = 2; j <= k; j++) res *= (n - j + 1) / j; return Math.round(res); }; binomialCoefficient(8, 2); // 28
• Nov 18, 2022 •AustinLeath
// Get the text that the user has selected const getSelectedText = () => window.getSelection().toString(); getSelectedText();
• Mar 11, 2021 •C S
require("dotenv").config(); const mongoose = require("mongoose"); const db = process.env.MONGO_URI; const connectDB = async () => { try { await mongoose.connect(db, { useNewUrlParser: true, useUnifiedTopology: true, useFindAndModify: false, useCreateIndex: true }); console.log("MongoDB Connected"); } catch (err) { console.error(err.message); } }; module.exports = connectDB;
• Apr 14, 2023 •Helper
0 likes • 8 views
const quickSort = arr => { const a = [...arr]; if (a.length < 2) return a; const pivotIndex = Math.floor(arr.length / 2); const pivot = a[pivotIndex]; const [lo, hi] = a.reduce( (acc, val, i) => { if (val < pivot || (val === pivot && i != pivotIndex)) { acc[0].push(val); } else if (val > pivot) { acc[1].push(val); } return acc; }, [[], []] ); return [...quickSort(lo), pivot, ...quickSort(hi)]; }; console.log(quickSort([1, 6, 1, 5, 3, 2, 1, 4])) // [1, 1, 1, 2, 3, 4, 5, 6]
• Jan 25, 2023 •C S
// 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(); }} />