Loading...
More JavaScript Posts
const heapsort = arr => {const a = [...arr];let l = a.length;const heapify = (a, i) => {const left = 2 * i + 1;const right = 2 * i + 2;let max = i;if (left < l && a[left] > a[max]) max = left;if (right < l && a[right] > a[max]) max = right;if (max !== i) {[a[max], a[i]] = [a[i], a[max]];heapify(a, max);}};for (let i = Math.floor(l / 2); i >= 0; i -= 1) heapify(a, i);for (i = a.length - 1; i > 0; i--) {[a[0], a[i]] = [a[i], a[0]];l--;heapify(a, 0);}return a;};heapsort([6, 3, 4, 1]); // [1, 3, 4, 6]
const getSearchTerm = delimiter => {let searchTerm = "";for (let i = 1; i < commands.length - 1; i++)searchTerm = searchTerm + commands[i] + delimiter;searchTerm += commands[commands.length - 1];return searchTerm;};
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();}}});}
// `arr` is an arrayconst clone = arr => arr.slice(0);// Orconst clone = arr => [...arr];// Orconst clone = arr => Array.from(arr);// Orconst clone = arr => arr.map(x => x);// Orconst clone = arr => JSON.parse(JSON.stringify(arr));// Orconst clone = arr => arr.concat([]);
//JavaScript program to swap two variables//take input from the userslet 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}`);
//JavaScript program to swap two variables//take input from the userslet a = prompt('Enter the first variable: ');let b = prompt('Enter the second variable: ');// XOR operatora = a ^ bb = a ^ ba = a ^ bconsole.log(`The value of a after swapping: ${a}`);console.log(`The value of b after swapping: ${b}`);