• Nov 19, 2022 •CodeCatch
0 likes • 0 views
const nums = [1, 2, 3]; const strs = Array.from('est'); nums.push(6); nums.push(4, 9); strs.unshift('t'); nums.length; // 6 nums[nums.length - 1]; // 9 strs[0]; // 't' strs[2]; // 's' nums.slice(1, 3); // [2, 3] nums.map(n => n * 2); // [2, 4, 6, 12, 8, 18] nums.filter(n => n % 2 === 0); // [2, 6, 4] nums.reduce((a, n) => a + n, 0); // 25 strs.reverse(); // ['t', 's', 'e', 't'] strs.join(''); // 'test' const nums = new Set([1, 2, 3]); nums.add(4); nums.add(1); nums.add(5); nums.add(4); nums.size; // 5 nums.has(4); // true nums.delete(4); nums.has(4); // false [...nums]; // [1, 2, 3, 5] nums.clear(); nums.size; // 0 const items = new Map([ [1, { name: 'John' }], [2, { name: 'Mary' }] ]); items.set(4, { name: 'Alan' }); items.set(2, { name: 'Jeff' }); items.size; // 3 items.has(4); // true items.get(2); // { name: 'Jeff' } items.delete(2); items.size; // 2 [...items.keys()]; // [1, 4] [...items.values()]; // [{ name: 'John' }, { name: 'Alan' }] items.clear(); items.size; // 0
• Nov 18, 2022 •AustinLeath
// Get the text that the user has selected const getSelectedText = () => window.getSelection().toString(); getSelectedText();
• Mar 22, 2022 •LeifMessinger
0 likes • 1 view
//QM Helper by Leif Messinger //Groups numbers by number of bits and shows their binary representations. //To be used on x.com const minterms = prompt("Enter your minterms separated by commas").split(",").map(x => parseInt(x.trim())); const maxNumBits = minterms.reduce(function(a, b) { return Math.max(a, Math.log2(b)); }); const bitGroups = []; for(let i = 0; i < maxNumBits; ++i){ bitGroups.push([]); } for(const minterm of minterms){ let outputString = (minterm+" "); //Count the bits let count = 0; for (var i = maxNumBits; i >= 0; --i) { if((minterm >> i) & 1){ ++count; outputString += "1"; }else{ outputString += "0"; } } bitGroups[count].push(outputString); } document.body.textContent = ""; document.body.style.setProperty("white-space", "pre"); for(const group of bitGroups){ for(const outputString of group){ document.body.textContent += outputString + "\r\n"; } }
0 likes • 2 views
const getURLParameters = url => (url.match(/([^?=&]+)(=([^&]*))/g) || []).reduce( (a, v) => ( (a[v.slice(0, v.indexOf('='))] = v.slice(v.indexOf('=') + 1)), a ), {} );
const luhnCheck = num => { let arr = (num + '') .split('') .reverse() .map(x => parseInt(x)); let lastDigit = arr.splice(0, 1)[0]; let sum = arr.reduce( (acc, val, i) => (i % 2 !== 0 ? acc + val : acc + ((val *= 2) > 9 ? val - 9 : val)), 0 ); sum += lastDigit; return sum % 10 === 0; }; luhnCheck('4485275742308327'); // true luhnCheck(6011329933655299); // true luhnCheck(123456789); // false
0 likes • 3 views
const reverse = str => str.split('').reverse().join(''); reverse('hello world'); // Result: 'dlrow olleh'