Loading...
More JavaScript Posts
const binarySearch = (arr, item) => {let l = 0,r = arr.length - 1;while (l <= r) {const mid = Math.floor((l + r) / 2);const guess = arr[mid];if (guess === item) return mid;if (guess > item) r = mid - 1;else l = mid + 1;}return -1;};binarySearch([1, 2, 3, 4, 5], 1); // 0binarySearch([1, 2, 3, 4, 5], 5); // 4binarySearch([1, 2, 3, 4, 5], 6); // -1
const arithmeticProgression = (n, lim) =>Array.from({ length: Math.ceil(lim / n) }, (_, i) => (i + 1) * n );arithmeticProgression(5, 25); // [5, 10, 15, 20, 25]
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 filesconsole.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 droppedconsole.log(files[0].kind);}});
const copyToClipboard = str => {const el = document.createElement('textarea');el.value = str;el.setAttribute('readonly', '');el.style.position = 'absolute';el.style.left = '-9999px';document.body.appendChild(el);const selected =document.getSelection().rangeCount > 0? document.getSelection().getRangeAt(0): false;el.select();document.execCommand('copy');document.body.removeChild(el);if (selected) {document.getSelection().removeAllRanges();document.getSelection().addRange(selected);}};copyToClipboard('Lorem ipsum'); // 'Lorem ipsum' copied to clipboard.
//use this with canvas file finderfunction NewTab(testing) {window.open(testing, "_blank");}for(test in results) {var testing = 'https://unt.instructure.com/files/' + test + '/download';NewTab(testing);}
class SequentialQueue{ //if you want it to go backwards, too badnext(){return this.i++;}constructor(start = 0){this.i = start;}}const que = new SequentialQueue(0);for(let i = 0; i < 10; i++){console.log(que.next());}