Skip to main content

doubly linked list

Nov 19, 2022CodeCatch
Loading...

More JavaScript Posts

function printHeap(heap, index, level) {
if (index >= heap.length) {
return;
}
console.log(" ".repeat(level) + heap[index]);
printHeap(heap, 2 * index + 1, level + 1);
printHeap(heap, 2 * index + 2, level + 1);
}
//You can call this function by passing in the heap array and the index of the root node, which is typically 0, and level = 0.
let heap = [3, 8, 7, 15, 17, 30, 35, 2, 4, 5, 9];
printHeap(heap,0,0)

return array head

Nov 19, 2022CodeCatch

0 likes • 0 views

const head = arr => (arr && arr.length ? arr[0] : undefined);
head([1, 2, 3]); // 1
head([]); // undefined
head(null); // undefined
head(undefined); // undefined

Invert Binary Tree

Oct 15, 2022CodeCatch

2 likes • 6 views

var invertTree = function(root) {
const reverseNode = node => {
if (node == null) {
return null
}
reverseNode(node.left);
reverseNode(node.right);
let holdLeft = node.left;
node.left = node.right;
node.right = holdLeft;
return node;
}
return reverseNode(root);
};

Authenticate Discord User

Mar 28, 2023Helper

0 likes • 1 view

const Discord = require('discord.js');
const client = new Discord.Client();
const token = 'YOUR_BOT_TOKEN';
// When the bot is ready, log a message to the console
client.on('ready', () => {
console.log(`Logged in as ${client.user.tag}!`);
});
// When a user sends a message, check if they are authenticated
client.on('message', async (msg) => {
if (!msg.author.bot && !msg.author.authenticated) {
const filter = (m) => m.author.id === msg.author.id;
const collector = msg.channel.createMessageCollector(filter, { time: 15000 });
// Send an authentication message to the user
msg.author.send('Please authenticate yourself by clicking this link: ' + generateAuthURL(msg.author.id));
collector.on('collect', async (m) => {
// Check if the message contains the authentication code
if (m.content.startsWith('!auth ')) {
const code = m.content.slice(6);
const { access_token } = await getAccessToken(code);
// Set the user's authenticated status and access token
msg.author.authenticated = true;
msg.author.access_token = access_token;
collector.stop();
}
});
collector.on('end', () => {
if (!msg.author.authenticated) {
msg.author.send('Authentication timed out.');
}
});
}
});
// Generate an authentication URL for the user
function generateAuthURL(userId) {
const redirectURI = encodeURIComponent('YOUR_REDIRECT_URL');
const scopes = 'identify';
const clientID = 'YOUR_CLIENT_ID';
return `https://discord.com/api/oauth2/authorize?client_id=${clientID}&redirect_uri=${redirectURI}&response_type=code&scope=${scopes}&state=${userId}`;
}
// Exchange the authorization code for an access token
async function getAccessToken(code) {
const redirectURI = encodeURIComponent('YOUR_REDIRECT_URL');
const clientID = 'YOUR_CLIENT_ID';
const clientSecret = 'YOUR_CLIENT_SECRET';
const res = await fetch('https://discord.com/api/oauth2/token', {
method: 'POST',
headers: {
'Content-Type': 'application/x-www-form-urlencoded',
},
body: `grant_type=authorization_code&code=${code}&redirect_uri=${redirectURI}&client_id=${clientID}&client_secret=${clientSecret}`,
});
return await res.json();
}
client.login(token);

Longest Palindromic Substring

Nov 19, 2022CodeCatch

0 likes • 1 view

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"))

Responsive Bootstrap Card.Img

Jan 25, 2023CHS

0 likes • 8 views

const ResponsiveCardImg = styled(Card.Img)`
// For screens wider than 768px
@media(min-width: 768px) {
// Your responsive styles here.
}
// For screens smaller than 768px
@media(max-width: 768px) {
// Your responsive styles here.
}
`
export default function App() {
return (
<MainContainer>
<Container>
<Card text='white' bg='dark' style={styles.mainCard}>
<ResponsiveCardImg
style={styles.cardImage}
onClick={handleClick}
src={apeImage}
alt='Degenerate Ape Academy'
/>
<Card.Body>
<Card.Title className='text-center' as='h2'>
{apeId}
</Card.Title>
</Card.Body>
</Card>
</Container>
</MainContainer>
);
}