This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import React, { useState, useEffect } from 'react'; | |
function MyComponent() { | |
const [data, setData] = useState(null); | |
useEffect(() => { | |
const fetchData = async () => { | |
const response = await fetch('https://myapi.com/data'); | |
const json = await response.json(); | |
setData(json); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
const MyComponent = React.lazy(() => import('./MyComponent')); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import React, { Suspense } from 'react'; | |
function App() { | |
return ( | |
<div> | |
<Suspense fallback={<div>Loading...</div>}> | |
<MyComponent /> | |
</Suspense> | |
</div> | |
); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
function findFirstRepeatingElement(arr) { | |
let count = {}; | |
for (let i = 0; i < arr.length; i++) { | |
if (count[arr[i]]) { | |
return arr[i]; | |
} else { | |
count[arr[i]] = 1; | |
} | |
} | |
return null; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// RECURSIVE APPROACH | |
function fibonacciRecursive(n) { | |
if (n <= 1) { | |
return n; | |
} else { | |
return fibonacciRecursive(n - 1) + fibonacciRecursive(n - 2); | |
} | |
} | |
console.log(fibonacciRecursive(7)); // Output: 13 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class Node { | |
constructor(data, next = null) { | |
this.data = data; | |
this.next = next; | |
} | |
} | |
class LinkedList { | |
constructor() { | |
this.head = null; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
function addTwoNumbers(l1, l2) { | |
let carry = 0; | |
let result = new ListNode(0); | |
let current = result; | |
while (l1 || l2) { | |
let sum = (l1 ? l1.val : 0) + (l2 ? l2.val : 0) + carry; | |
carry = sum >= 10 ? 1 : 0; | |
current.next = new ListNode(sum % 10); | |
current = current.next; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
function generatePermutations(str) { | |
let result = []; | |
function permute(str, prefix = '') { | |
if (str.length === 0) { | |
result.push(prefix); | |
} else { | |
for (let i = 0; i < str.length; i++) { | |
let char = str[i]; | |
let remaining = str.slice(0, i) + str.slice(i + 1); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class Stack { | |
constructor() { | |
this.items = []; | |
this.min = Infinity; | |
} | |
push(element) { | |
if (element < this.min) { | |
this.min = element; | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class TreeNode { | |
constructor(val) { | |
this.val = val; | |
this.left = null; | |
this.right = null; | |
} | |
} | |
function diameterOfBinaryTree(root) { | |
let diameter = 0; |
NewerOlder