• Oct 15, 2022 •CodeCatch
0 likes • 164 views
import { useState, useEffect, useRef } from "react"; // Usage function App() { // State value and setter for our example const [count, setCount] = useState(0); // Get the previous value (was passed into hook on last render) const prevCount = usePrevious(count); // Display both current and previous count value return ( <div> <h1> Now: {count}, before: {prevCount} </h1> <button onClick={() => setCount(count + 1)}>Increment</button> </div> ); } // Hook function usePrevious(value) { // The ref object is a generic container whose current property is mutable ... // ... and can hold any value, similar to an instance property on a class const ref = useRef(); // Store current value in ref useEffect(() => { ref.current = value; }, [value]); // Only re-run if value changes // Return previous value (happens before update in useEffect above) return ref.current; }
• Sep 19, 2024 •Sam-C137
0 likes • 2 views
class DFS { public dfs(G: DFSGraph, startVert: number) { let visited: boolean[] = Array<boolean>(); // Pre-populate array: for(let i = 0; i < G.size; i++) { visited.push(false); } let s: number[] = new Array(); visited[startVert] = true; s.push(startVert); while(s.length > 0) { const v = s.pop(); for(let adjV of G.adj[v]) { if(!visited[adjV]) { visited[adjV] = true; s.push(adjV); } } } } public dfsRecursive(G: DFSGraph, startVert: number) { let visited: boolean[] = Array<boolean>(); // Pre-populate array: for(let i = 0; i < G.size; i++) { visited.push(false); } this.dfsAux(G, startVert, visited); } private dfsAux(G: DFSGraph, v: number, visited: boolean[]) { visited[v] = true; for(let adjV of G.adj[v]) { if(!visited[adjV]) { // this.foo(); // Something can happen before the visit. this.dfsAux(G, adjV, visited); // this.bar(); // Something can happen after the visit. } } } }
• Mar 31, 2023 •Helper
0 likes • 5 views
const langToConfig = { 'javascript': { image: '', cmd: [''] }, 'php': { image: '', cmd: [''] }, }
• Oct 22, 2024 •C S
function twoSum(nums: number[], target: number): number[] { const map = new Map(); for(let i = 0; i < nums.length; i++) { if(map.has(target - nums[i])) { return [map.get(target - nums[i]), i]; } else { map.set(nums[i], i); } } return [] } console.log(twoSum([1, 2, 3, 4, 5], 3))
• Jun 1, 2023 •CodeCatch
interface Task { id: number; title: string; completed: boolean; } class TaskManager { private tasks: Task[] = []; constructor() {} addTask(title: string) { const taskId = this.tasks.length + 1; const task: Task = { id: taskId, title, completed: false, }; this.tasks.push(task); } markTaskAsComplete(taskId: number) { const task = this.tasks.find((task) => task.id === taskId); if (task) { task.completed = true; } } markTaskAsIncomplete(taskId: number) { const task = this.tasks.find((task) => task.id === taskId); if (task) { task.completed = false; } } listTasks() { console.log('Tasks:'); this.tasks.forEach((task) => { console.log(`${task.id}. [${task.completed ? 'X' : ' '}] ${task.title}`); }); } } // Example usage: const taskManager = new TaskManager(); taskManager.addTask('Buy groceries'); taskManager.addTask('Pay bills'); taskManager.addTask('Clean the house'); taskManager.listTasks(); // Output: // Tasks: // 1. [ ] Buy groceries // 2. [ ] Pay bills // 3. [ ] Clean the house
0 likes • 88 views