Loading...
More JavaScript Posts
"use strict";const nodemailer = require("nodemailer");// async..await is not allowed in global scope, must use a wrapperasync function main() {// Generate test SMTP service account from ethereal.email// Only needed if you don't have a real mail account for testinglet testAccount = await nodemailer.createTestAccount();// create reusable transporter object using the default SMTP transportlet transporter = nodemailer.createTransport({host: "smtp.ethereal.email",port: 587,secure: false, // true for 465, false for other portsauth: {user: testAccount.user, // generated ethereal userpass: testAccount.pass, // generated ethereal password},});// send mail with defined transport objectlet info = await transporter.sendMail({subject: "Hello ✔", // Subject linetext: "Hello world?", // plain text bodyhtml: "<b>Hello world?</b>", // html body});console.log("Message sent: %s", info.messageId);// Message sent: <[email protected]>// Preview only available when sending through an Ethereal accountconsole.log("Preview URL: %s", nodemailer.getTestMessageUrl(info));// Preview URL: https://ethereal.email/message/WaQKMgKddxQDoou...}main().catch(console.error);
const longestPalindrome = (s) => {if(s.length === 1) return sconst odd = longestOddPalindrome(s)const even = longestEvenPalindrome(s)if(odd.length >= even.length) return oddif(even.length > odd.length) return even};const longestOddPalindrome = (s) => {let longest = 0let longestSubStr = ""for(let i = 1; i < s.length; i++) {let currentLongest = 1let currentLongestSubStr = ""let left = i - 1let right = i + 1while(left >= 0 && right < s.length) {const leftLetter = s[left]const rightLetter = s[right]left--right++if(leftLetter === rightLetter) {currentLongest += 2currentLongestSubStr = s.slice(left+1, right)} else break}if(currentLongest > longest) {if(currentLongestSubStr) longestSubStr = currentLongestSubStrelse longestSubStr = s[i]longest = currentLongest}}return longestSubStr}const longestEvenPalindrome = (s) => {let longest = 0let longestSubStr = ""for(let i = 0; i < s.length - 1; i++) {if(s[i] !== s[i+1]) continue;let currentLongest = 2let currentLongestSubStr = ""let left = i - 1let right = i + 2while(left >= 0 && right < s.length) {const leftLetter = s[left]const rightLetter = s[right]left--right++if(leftLetter === rightLetter) {currentLongest += 2currentLongestSubStr = s.slice(left+1, right)} else break}if(currentLongest > longest) {if(currentLongestSubStr) longestSubStr = currentLongestSubStrelse longestSubStr = s[i] + s[i+1]longest = currentLongest}}return longestSubStr}console.log(longestPalindrome("babad"))
const insertionSort = arr =>arr.reduce((acc, x) => {if (!acc.length) return [x];acc.some((y, j) => {if (x <= y) {acc.splice(j, 0, x);return true;}if (x > y && j === acc.length - 1) {acc.splice(j + 1, 0, x);return true;}return false;});return acc;}, []);insertionSort([6, 3, 4, 1]); // [1, 3, 4, 6]
const nums = [1, 2, 3];const strs = Array.from('est');nums.push(6);nums.push(4, 9);strs.unshift('t');nums.length; // 6nums[nums.length - 1]; // 9strs[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); // 25strs.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; // 5nums.has(4); // truenums.delete(4);nums.has(4); // false[...nums]; // [1, 2, 3, 5]nums.clear();nums.size; // 0const items = new Map([[1, { name: 'John' }],[2, { name: 'Mary' }]]);items.set(4, { name: 'Alan' });items.set(2, { name: 'Jeff' });items.size; // 3items.has(4); // trueitems.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
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 express = require('express')const app = express()const port = 3000app.get('/', (req, res) => {res.send('Hello World!')})app.listen(port, () => {console.log(`Example app listening on port ${port}`)})