Skip to main content

LeetCode Contains Duplicate

Sep 13, 2023C S
Loading...

More JavaScript Posts

Send mail with nodemailer

Nov 18, 2022AustinLeath

0 likes • 1 view

"use strict";
const nodemailer = require("nodemailer");
// async..await is not allowed in global scope, must use a wrapper
async function main() {
// Generate test SMTP service account from ethereal.email
// Only needed if you don't have a real mail account for testing
let testAccount = await nodemailer.createTestAccount();
// create reusable transporter object using the default SMTP transport
let transporter = nodemailer.createTransport({
host: "smtp.ethereal.email",
port: 587,
secure: false, // true for 465, false for other ports
auth: {
user: testAccount.user, // generated ethereal user
pass: testAccount.pass, // generated ethereal password
},
});
// send mail with defined transport object
let info = await transporter.sendMail({
from: '"Fred Foo 👻" <[email protected]>', // sender address
to: "[email protected], [email protected]", // list of receivers
subject: "Hello ✔", // Subject line
text: "Hello world?", // plain text body
html: "<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 account
console.log("Preview URL: %s", nodemailer.getTestMessageUrl(info));
// Preview URL: https://ethereal.email/message/WaQKMgKddxQDoou...
}
main().catch(console.error);

Longest Palindromic Substring

Nov 19, 2022CodeCatch

0 likes • 2 views

const longestPalindrome = (s) => {
if(s.length === 1) return s
const odd = longestOddPalindrome(s)
const even = longestEvenPalindrome(s)
if(odd.length >= even.length) return odd
if(even.length > odd.length) return even
};
const longestOddPalindrome = (s) => {
let longest = 0
let longestSubStr = ""
for(let i = 1; i < s.length; i++) {
let currentLongest = 1
let currentLongestSubStr = ""
let left = i - 1
let right = i + 1
while(left >= 0 && right < s.length) {
const leftLetter = s[left]
const rightLetter = s[right]
left--
right++
if(leftLetter === rightLetter) {
currentLongest += 2
currentLongestSubStr = s.slice(left+1, right)
} else break
}
if(currentLongest > longest) {
if(currentLongestSubStr) longestSubStr = currentLongestSubStr
else longestSubStr = s[i]
longest = currentLongest
}
}
return longestSubStr
}
const longestEvenPalindrome = (s) => {
let longest = 0
let longestSubStr = ""
for(let i = 0; i < s.length - 1; i++) {
if(s[i] !== s[i+1]) continue;
let currentLongest = 2
let currentLongestSubStr = ""
let left = i - 1
let right = i + 2
while(left >= 0 && right < s.length) {
const leftLetter = s[left]
const rightLetter = s[right]
left--
right++
if(leftLetter === rightLetter) {
currentLongest += 2
currentLongestSubStr = s.slice(left+1, right)
} else break
}
if(currentLongest > longest) {
if(currentLongestSubStr) longestSubStr = currentLongestSubStr
else longestSubStr = s[i] + s[i+1]
longest = currentLongest
}
}
return longestSubStr
}
console.log(longestPalindrome("babad"))

insertion sort

Nov 19, 2022CodeCatch

0 likes • 0 views

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]

native data structures

Nov 19, 2022CodeCatch

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

List Largest Files Google Drive

Nov 16, 2023LeifMessinger

0 likes • 6 views

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 iterator
parents.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] entries
const filesMaxSize = 10;
while (fileIterator.hasNext()) {
var file = fileIterator.next();
const owner = file.getOwner();
//Only files I own
if((!(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 1
const nextFile = arr[i-1];
if(nextFile[1] > entry[1]){
break;
}else{
//Swap with the next file to slide the file up
const 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 file
slideUp(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());
}
}

Simple Express Server

Oct 15, 2022CodeCatch

1 like • 187 views

const express = require('express')
const app = express()
const port = 3000
app.get('/', (req, res) => {
res.send('Hello World!')
})
app.listen(port, () => {
console.log(`Example app listening on port ${port}`)
})