Skip to main content

UNT canvas file finder utility

0 likes • Nov 18, 2022 • 0 views
JavaScript
Loading...

More JavaScript Posts

return array head

0 likes • Nov 19, 2022 • 0 views
JavaScript
const head = arr => (arr && arr.length ? arr[0] : undefined);
head([1, 2, 3]); // 1
head([]); // undefined
head(null); // undefined
head(undefined); // undefined

Rings and Rods

0 likes • Nov 19, 2022 • 1 view
JavaScript
// There are n rings and each ring is either red, green, or blue. The rings are distributed across ten rods labeled from 0 to 9.
// You are given a string rings of length 2n that describes the n rings that are placed onto the rods. Every two characters in rings forms a color-position pair that is used to describe each ring where:
// The first character of the ith pair denotes the ith ring's color ('R', 'G', 'B').
// The second character of the ith pair denotes the rod that the ith ring is placed on ('0' to '9').
// For example, "R3G2B1" describes n == 3 rings: a red ring placed onto the rod labeled 3, a green ring placed onto the rod labeled 2, and a blue ring placed onto the rod labeled 1.
// Return the number of rods that have all three colors of rings on them.
let rings = "B0B6G0R6R0R6G9";
var countPoints = function(rings) {
let sum = 0;
// Always 10 Rods
for (let i = 0; i < 10; i++) {
if (rings.includes(`B${i}`) && rings.includes(`G${i}`) && rings.includes(`R${i}`)) {
sum+=1;
}
}
return sum;
};
console.log(countPoints(rings));

quick sort

0 likes • Nov 19, 2022 • 0 views
JavaScript
const quickSort = arr => {
const a = [...arr];
if (a.length < 2) return a;
const pivotIndex = Math.floor(arr.length / 2);
const pivot = a[pivotIndex];
const [lo, hi] = a.reduce(
(acc, val, i) => {
if (val < pivot || (val === pivot && i != pivotIndex)) {
acc[0].push(val);
} else if (val > pivot) {
acc[1].push(val);
}
return acc;
},
[[], []]
);
return [...quickSort(lo), pivot, ...quickSort(hi)];
};
quickSort([1, 6, 1, 5, 3, 2, 1, 4]); // [1, 1, 1, 2, 3, 4, 5, 6]

List Largest Files Google Drive

0 likes • Nov 16, 2023 • 5 views
JavaScript
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());
}
}

Mongoose Model

CHS
0 likes • Mar 11, 2021 • 0 views
JavaScript
const mongoose = require('mongoose')
const UserSchema = new mongoose.Schema({
username: {
type: String,
required: true
},
email: {
type: String,
unique: true,
required: true
},
password: {
type: String,
required: true
},
date: {
type: Date,
default: Date.now
}
})
module.exports = User = mongoose.model('user', UserSchema)

Simple Express Server

0 likes • Oct 15, 2022 • 66 views
JavaScript
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}`)
})