Skip to main content

find primes

0 likes • Nov 19, 2022 • 0 views
JavaScript
Loading...

More JavaScript Posts

Responsive Bootstrap Card.Img

CHS
0 likes • Jan 25, 2023 • 2 views
JavaScript
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>
);
}

Ticking clock

0 likes • Nov 19, 2022 • 0 views
JavaScript
function Clock(props) {
return (
<div>
<h1>Hello, world!</h1>
<h2>It is {props.date.toLocaleTimeString()}.</h2>
</div>
);
}
function tick() {
ReactDOM.render(
<Clock date={new Date()} />,
document.getElementById('root')
);
}
setInterval(tick, 1000);

Destructuring Assignment

0 likes • Nov 19, 2022 • 0 views
JavaScript
//JavaScript program to swap two variables
//take input from the users
let a = prompt('Enter the first variable: ');
let b = prompt('Enter the second variable: ');
//using destructuring assignment
[a, b] = [b, a];
console.log(`The value of a after swapping: ${a}`);
console.log(`The value of b after swapping: ${b}`);

Tree Traversal

CHS
0 likes • Oct 16, 2023 • 7 views
JavaScript
class TreeNode {
constructor(data, depth) {
this.data = data;
this.children = [];
this.depth = depth;
}
}
function buildNaryTree(hosts) {
const nodeMap = {};
// Step 1: Create a map of nodes using fqdn as the key
hosts.forEach((host) => {
nodeMap[host.fqdn] = new TreeNode(host, 0); // Initialize depth to 0
});
// Step 2: Iterate through the array and add each node to its parent's list of children
hosts.forEach((host) => {
if (host.displayParent) {
if (nodeMap[host.displayParent]) {
const parent = nodeMap[host.displayParent];
const node = nodeMap[host.fqdn];
node.depth = parent.depth + 1; // Update the depth
parent.children.push(node);
} else {
console.error(`Parent with fqdn ${host.displayParent} not found for ${host.fqdn}`);
}
}
});
// Find the root nodes (nodes with no parent)
const rootNodes = hosts.filter((host) => !host.displayParent).map((host) => nodeMap[host.fqdn]);
return rootNodes;
}
function treeToObjects(node) {
const result = [];
function inOrder(node) {
if (!node) {
return;
}
// Visit the current node and add it to the result
result.push(node.data);
// Visit children nodes first
node.children.forEach((child) => {
inOrder(child);
});
}
inOrder(node);
return result;
}
// Usage example
const hosts = [
{
fqdn: 'fqdn_1',
display_name: 'Host 1',
},
{
fqdn: 'fqdn_2',
display_name: 'Host 2',
displayParent: 'fqdn_1',
},
{
fqdn: 'fqdn_3',
display_name: 'Host 3'
},
{
fqdn: 'fqdn_4',
display_name: 'Host 4',
displayParent: 'fqdn_3',
},
{
fqdn: 'fqdn_5',
display_name: 'Host 5',
displayParent: 'fqdn_1',
},
];
const tree = buildNaryTree(hosts);
// Function to convert the tree to an array of objects
function convertTreeToArray(nodes) {
const result = [];
nodes.forEach((node) => {
result.push(...treeToObjects(node));
});
return result;
}
// Convert the tree back to an array of objects
const arrayFromTree = convertTreeToArray(tree);
console.log(arrayFromTree);

Longest Palindromic Substring

0 likes • Nov 19, 2022 • 0 views
JavaScript
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"))

drag and drop uploader

0 likes • Nov 18, 2022 • 0 views
JavaScript
const dragAndDropDiv = editor.container;
dragAndDropDiv.addEventListener('dragover', function(e) {
e.stopPropagation();
e.preventDefault();
e.dataTransfer.dropEffect = 'copy';
});
dragAndDropDiv.addEventListener("drop",function(e){
// Prevent default behavior (Prevent file from being opened)
e.stopPropagation();
e.preventDefault();
const files = e.dataTransfer.items; // Array of all files
console.assert(files.length >= 1);
if (files[0].kind === 'file') {
var file = e.dataTransfer.items[0].getAsFile();
const fileSize = file.size;
const fileName = file.name;
const fileMimeType = file.type;
reader = new FileReader();
reader.onloadend = function(){
editor.setValue(reader.result);
}
reader.readAsText(file);
}else{
//Maybe handle if text is dropped
console.log(files[0].kind);
}
});