• 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 5, 2026 •C S
0 likes • 9 views
<div className="md:self-start sticky top-16 bg-background px-4 mt-1 sm:px-6 md:pt-4 lg:pl-8 lg:pr-4"> <ScrollArea className="whitespace-nowrap"> <div className="w-max flex md:flex-col gap-1 bg-transparent text-muted-foreground pb-2"> {menuData.map((section) => ( <Button key={section.title} onClick={() => handleScrollToSection(section.title)} variant="ghost" size="sm" className={cn( 'w-auto md:w-full hover:cursor-pointer relative md:justify-end', activeSection === section.title ? cn( 'text-foreground after:absolute after:bottom-[-2px] after:bottom-0 after:w-full after:h-[2px] after:bg-primary', 'md:after:right-[-2px] md:after:top-0 md:after:h-full md:after:w-[2px]', ) : '', )} > {section.title} </Button> ))} </div> <ScrollBar orientation="horizontal" /> </ScrollArea> </div>
0 likes • 3 views
const linearSearch = (arr, item) => { for (const i in arr) { if (arr[i] === item) return +i; } return -1; }; linearSearch([2, 9, 9], 9); // 1 linearSearch([2, 9, 9], 7); // -1
• Feb 3, 2021 •C S
0 likes • 1 view
import React, { useState } from 'react' import Welcome from '../components/Welcome' function About() { const [showWelcome, setShowWelcome] = useState(false) return ( <div> {showWelcome ? <Welcome /> : null} </div> <div> {showWelcome && <Welcome /> } </div> ) } export default App
0 likes • 2 views
// `arr` is an array const clone = arr => arr.slice(0); // Or const clone = arr => [...arr]; // Or const clone = arr => Array.from(arr); // Or const clone = arr => arr.map(x => x); // Or const clone = arr => JSON.parse(JSON.stringify(arr)); // Or const clone = arr => arr.concat([]);
• 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(); });