Skip to main content

Untitled

CAS
1 like • Aug 13, 2023 • 2 views
JavaScript
Loading...

More JavaScript Posts

unzipWith() function

0 likes • Nov 19, 2022 • 0 views
JavaScript
const unzipWith = (arr, fn) =>
arr
.reduce(
(acc, val) => (val.forEach((v, i) => acc[i].push(v)), acc),
Array.from({
length: Math.max(...arr.map(x => x.length))
}).map(x => [])
)
.map(val => fn(...val));
unzipWith(
[
[1, 10, 100],
[2, 20, 200],
],
(...args) => args.reduce((acc, v) => acc + v, 0)
);
// [3, 30, 300]

Clone an array

0 likes • Nov 19, 2022 • 0 views
JavaScript
// `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([]);

typewriter-effect in Vanilla JS and React

CHS
0 likes • Jan 25, 2023 • 7 views
JavaScript
// Vanilla JS Solution:
var app = document.getElementById('app');
var typewriter = new Typewriter(app, { loop: true });
typewriter
.typeString("I'm John and I'm a super cool web developer")
.pauseFor(3000)
.deleteChars(13) // "web developer" = 13 characters
.typeString("person to talk with!") // Will display "I'm John and I'm a super cool person to talk with!"
.start();
// React Solution:
import Typewriter from 'typewriter-effect';
<Typewriter
options={{ loop: true }}
onInit={typewriter => {
typewriter
.typeString("I'm John and I'm a super cool web developer")
.pauseFor(3000)
.deleteChars(13) // "web developer" = 13 characters
.typeString("person to talk with!") // Will display "I'm John and I'm a super cool person to talk with!"
.start();
}}
/>

deep merge objects

0 likes • Nov 19, 2022 • 2 views
JavaScript
const deepMerge = (a, b, fn) =>
[...new Set([...Object.keys(a), ...Object.keys(b)])].reduce(
(acc, key) => ({ ...acc, [key]: fn(key, a[key], b[key]) }),
{}
);
deepMerge(
{ a: true, b: { c: [1, 2, 3] } },
{ a: false, b: { d: [1, 2, 3] } },
(key, a, b) => (key === 'a' ? a && b : Object.assign({}, a, b))
);
// { a: false, b: { c: [ 1, 2, 3 ], d: [ 1, 2, 3 ] } }

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);
}
});

Invert Binary Tree

0 likes • Oct 15, 2022 • 6 views
JavaScript
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);
};