Loading...
More JavaScript Posts
# Acronyms## Computer Numerical Control (CNC)- Typicalldfdfsdffdafdasgfdgfgfdfdafdasfdafda
export const Main = () => {const [title, setTitle] = useState('');return (<ChildComponenttitle={title}onChange={e => setTitle(e.target.value)}/>);};export const ChildComponent = ({ title, onChange }) => {return (<Box component="form" onSubmit={() => {}} noValidate mt={5}><TextFieldfullWidthid="title"label="Title"name="title"autoComplete="title"autoFocuscolor="primary"variant="filled"InputLabelProps={{ shrink: true }}value={title}onChange={onChange}/></Box>);};
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();}}});}
//Retries maxRetries number of times, meaning that if you have 0, then it'll run the function once.async function retryPromise(promiseFn, maxRetries = 3, delayOffset = 0, delayRandomRange = 0) {return new Promise((resolve, reject) => {promiseFn().then(resolve, (error) => { //On errorif (maxRetries <= 0) {return reject(error);} else {if(delayRandomRange * Math.random() + delayOffset < 1.0){return retryPromise(promiseFn, maxRetries - 1, delayOffset, delayRandomRange).then(resolve, reject);}else{return new Promise((resolveTwo, rejectTwo) => {setTimeout(() => {return retryPromise(promiseFn, maxRetries - 1, delayOffset, delayRandomRange).then(resolveTwo, rejectTwo);}, delayRandomRange * Math.random() + delayOffset);}).then(resolve, reject);}}});});}//Tests for that function.//Returns a function that will fail numTimes times, then will return aValuefunction functionThatReturnsAFunctionThatFailsACoupleTimes(numTimes, aValue){return async function(){if(numTimes == 0){return aValue;}numTimes--;throw false;};}const SMALL_NUMBER = 10;function testTest(failNumberOfTimes){let functionThatFailsACoupleTimes = functionThatReturnsAFunctionThatFailsACoupleTimes(failNumberOfTimes, true);//Test my testfor(let i = 0; i < (failNumberOfTimes * 2); ++i){function evalTest(val){let testTestFailedReason = "";if(val === undefined){testTestFailedReason = "Test test returned undefined for some reason.";}if((i + 1) > failNumberOfTimes){if(val === true){//We're good}else{testTestFailedReason = "Test test didn't return true when it should have.";}}else{if(val === false){//We're good}else{testTestFailedReason = "Test test didn't return false when it should have.";}}testTestFailedReason = testTestFailedReason || "Test test passed test case";console.log(testTestFailedReason, "at index", i, "where the function returned", val);}functionThatFailsACoupleTimes().then(evalTest, evalTest)};}testTest(SMALL_NUMBER);let testCaseCounter = 1;const throwsNoError = [(val)=>{if(val == true){console.log("Passed test case " + (testCaseCounter++));}else{console.error("Unexpected return value", val);}},()=>{console.error("It wasn't supposed to fail!")}]const throwsError = [(val)=>{console.error("It wasn't supposed to succeed!", val);},(val)=>{if(val == false){console.log("Passed test case " + (testCaseCounter++));}else{console.error("Unexpected return value", val);}}];//Runs SMALL_NUMBER times, because SMALL_NUMBER - 1 is the number of retries after the first onelet functionThatFailsACoupleTimes = functionThatReturnsAFunctionThatFailsACoupleTimes(SMALL_NUMBER - 1, true);retryPromise(functionThatFailsACoupleTimes, SMALL_NUMBER - 1).then(...throwsNoError)functionThatFailsACoupleTimes = functionThatReturnsAFunctionThatFailsACoupleTimes(SMALL_NUMBER - 1, true);//Runs SMALL_NUMBER - 1 times, because SMALL_NUMBER - 2 is the number of retries after the first oneretryPromise(functionThatFailsACoupleTimes, SMALL_NUMBER - 2).then(...throwsError)//Testing the delay. You'll have to wait a bit too.functionThatFailsACoupleTimes = functionThatReturnsAFunctionThatFailsACoupleTimes(SMALL_NUMBER - 1, true);//Runs SMALL_NUMBER times, because SMALL_NUMBER - 1 is the number of retries after the first oneretryPromise(functionThatFailsACoupleTimes, SMALL_NUMBER - 1, 500, 500).then(...throwsNoError)functionThatFailsACoupleTimes = functionThatReturnsAFunctionThatFailsACoupleTimes(SMALL_NUMBER - 1, true);//Runs SMALL_NUMBER - 1 times, because SMALL_NUMBER - 2 is the number of retries after the first oneretryPromise(functionThatFailsACoupleTimes, SMALL_NUMBER - 2, 500, 500).then(...throwsError)
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'); // trueluhnCheck(6011329933655299); // trueluhnCheck(123456789); // false
function printHeap(heap, index, level) {if (index >= heap.length) {return;}console.log(" ".repeat(level) + heap[index]);printHeap(heap, 2 * index + 1, level + 1);printHeap(heap, 2 * index + 2, level + 1);}//You can call this function by passing in the heap array and the index of the root node, which is typically 0, and level = 0.let heap = [3, 8, 7, 15, 17, 30, 35, 2, 4, 5, 9];printHeap(heap,0,0)