Loading...
More JavaScript Posts
const union = (...arr) => [...new Set(arr.flat())];// Exampleunion([1, 2], [2, 3], [3]); // [1, 2, 3]
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);};
const MongoClient = require('mongodb').MongoClient;const assert = require('assert');// Connection URLconst url = 'mongodb://localhost:27017';// Database Nameconst dbName = 'myproject';// Use connect method to connect to the serverMongoClient.connect(url, function(err, client) {assert.equal(null, err);console.log("Connected successfully to server");const db = client.db(dbName);client.close();});
// Time Complexity : O(N)// Space Complexity : O(1)var isMonotonic = function(nums) {let isMono = null;for(let i = 1; i < nums.length; i++) {if(isMono === null) {if(nums[i - 1] < nums[i]) isMono = 0;else if(nums[i - 1] > nums[i]) isMono = 1;continue;}if(nums[i - 1] < nums[i] && isMono !== 0) {return false;}else if(nums[i - 1] > nums[i] && isMono !== 1) {return false;}}return true;};let nums1 = [1,2,2,3]let nums2 = [6,5,4,4]let nums3 = [1,3,2]console.log(isMonotonic(nums1));console.log(isMonotonic(nums2));console.log(isMonotonic(nums3));
const euclideanDistance = (a, b) =>Math.hypot(...Object.keys(a).map(k => b[k] - a[k]));euclideanDistance([1, 1], [2, 3]); // ~2.2361euclideanDistance([1, 1, 1], [2, 3, 2]); // ~2.4495
function copyString(text){async function navigatorClipboardCopy(text){if(document.location.protocol != "https:"){return false;}return new Promise((resolve, reject)=>{navigator.clipboard.writeText(text).then(()=>{resolve(true);}, ()=>{resolve(false);});});}function domCopy(text){if(!(document.execCommand)){console.warn("They finally deprecated document.execCommand!");}const input = document.createElement('textarea');input.value = text;document.body.appendChild(input);input.select();const success = document.execCommand('copy');document.body.removeChild(input);return success;}function promptCopy(){prompt("Copy failed. Might have ... somewhere. Check console.", text);console.log(text);}function done(){alert("Copied to clipboard");}navigatorClipboardCopy(text).catch(()=>{return false;}).then((success)=>{if(success){done();}else{if(domCopy(text)){done();}else{promptCopy();}}});}