Loading...
More JavaScript Posts
const primes = num => {let arr = Array.from({ length: num - 1 }).map((x, i) => i + 2),sqroot = Math.floor(Math.sqrt(num)),numsTillSqroot = Array.from({ length: sqroot - 1 }).map((x, i) => i + 2);numsTillSqroot.forEach(x => (arr = arr.filter(y => y % x !== 0 || y === x)));return arr;};primes(10); // [2, 3, 5, 7]
const countSubstrings = (str, searchValue) => {let count = 0,i = 0;while (true) {const r = str.indexOf(searchValue, i);if (r !== -1) [count, i] = [count + 1, r + 1];else return count;}};countSubstrings('tiktok tok tok tik tok tik', 'tik'); // 3countSubstrings('tutut tut tut', 'tut'); // 4
const getSubsets = arr => arr.reduce((prev, curr) => prev.concat(prev.map(k => k.concat(curr))), [[]]);// ExamplesgetSubsets([1, 2]); // [[], [1], [2], [1, 2]]getSubsets([1, 2, 3]); // [[], [1], [2], [1, 2], [3], [1, 3], [2, 3], [1, 2, 3]]
function filePath(file){let folders = file.getParents();const parents = [];function makePathString(folderArray){let path = "";folderArray.forEach((folder)=>{path += "/" + folder.getName();});return path;}while (folders.hasNext()) {const folder = folders.next(); //This should hopefully remove that folder from the iteratorparents.unshift(folder);}return makePathString(parents);}function myFunction() {const myEmail = Session.getEffectiveUser().getEmail();Logger.log("My email is " + myEmail);// Log the name of every file in the user's Drive.var fileIterator = DriveApp.getFiles();const files = []; //List of [File, size] entriesconst filesMaxSize = 10;while (fileIterator.hasNext()) {var file = fileIterator.next();const owner = file.getOwner();//Only files I ownif((!(owner)) || (!(owner.getEmail)) || owner.getEmail() != myEmail){continue;}const entry = [file, file.getSize()];function slideUp(arr, index){//Let's keep sliding it up so we don't have to sort it at the end.for(let i = index; i > 0; --i){ //Stops at 1const nextFile = arr[i-1];if(nextFile[1] > entry[1]){break;}else{//Swap with the next file to slide the file upconst temp = arr[i];arr[i] = arr[i - 1];arr[i - 1] = temp;}}}if(files.length < filesMaxSize){files.push(entry);slideUp(files, files.length - 1);}else{if(entry[1] > files[files.length - 1][1]){ //If it's bigger than the smallest file in the list.files[files.length - 1] = entry; //Replace the smallest fileslideUp(files, files.length - 1); //Slide it up}}}for(let i = 0; i < filesMaxSize; ++i){const file = files[i][0];const size = files[i][1];Logger.log(size + "\t" + filePath(file) + "/" + file.getName() + "\t" + file.getOwner().getName());}}
const head = arr => (arr && arr.length ? arr[0] : undefined);head([1, 2, 3]); // 1head([]); // undefinedhead(null); // undefinedhead(undefined); // undefined
// Get the text that the user has selectedconst getSelectedText = () => window.getSelection().toString();getSelectedText();