Skip to main content

binomial coefficient

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

More JavaScript Posts

Monotonic Array

0 likes • Nov 19, 2022 • 2 views
JavaScript
// Time Complexity : O(N)
// Space Complexity : O(1)
var isMonotonic = function(nums) {
let isMono = null;
for(let i = 1; i < nums.length; i++) {
if(isMono === null) {
if(nums[i - 1] < nums[i]) isMono = 0;
else if(nums[i - 1] > nums[i]) isMono = 1;
continue;
}
if(nums[i - 1] < nums[i] && isMono !== 0) {
return false;
}
else if(nums[i - 1] > nums[i] && isMono !== 1) {
return false;
}
}
return true;
};
let nums1 = [1,2,2,3]
let nums2 = [6,5,4,4]
let nums3 = [1,3,2]
console.log(isMonotonic(nums1));
console.log(isMonotonic(nums2));
console.log(isMonotonic(nums3));

format duration

0 likes • Nov 19, 2022 • 0 views
JavaScript
const formatDuration = ms => {
if (ms < 0) ms = -ms;
const time = {
day: Math.floor(ms / 86400000),
hour: Math.floor(ms / 3600000) % 24,
minute: Math.floor(ms / 60000) % 60,
second: Math.floor(ms / 1000) % 60,
millisecond: Math.floor(ms) % 1000
};
return Object.entries(time)
.filter(val => val[1] !== 0)
.map(([key, val]) => `${val} ${key}${val !== 1 ? 's' : ''}`)
.join(', ');
};
formatDuration(1001); // '1 second, 1 millisecond'
formatDuration(34325055574); // '397 days, 6 hours, 44 minutes, 15 seconds, 574 milliseconds'

Canvas File Downloader

0 likes • Nov 18, 2022 • 0 views
JavaScript
results =
{
"11345240":"Media Comment-80e47004-1719-493a-af69-a43b2652b41c",
"11345282":"zoom_3_22_21.mp4",
"11345303":"Recording-2.m4a",
"11345348":"February 12%2C 2021.mp4",
"11345826":"Lecture_Rousso_Chapter_06-2.pptx",
"11345933":"Themes_InClassAssigment.pptx",
"11346193":"Agenda – Week of March23.pptx",
"11346318":"MUJS 2370 SP2021 Updated Rhythm Changes Drills Bb - Score.pdf",
"11346319":"MUJS 2370 SP2021 Updated Rhythm Changes Drills C - Score.pdf",
"11346322":"MUJS 2370 SP2021 Updated Rhythm Changes Drills Eb - Score.pdf",
"11346323":"MUJS 2370 Updated Rhythm Changes Drills Bass - Score.pdf",
"11346381":"Anthropology with colored highlighter analysis - Mar 22 2021 - 10-36 AM.pdf",
"11346449":"Anthropology with colored highlighter analysis - Mar 22 2021 - 10-36 AM.pdf",
"11346450":"MUJS 2370 SP2021 Updated Rhythm Changes Drills Bb - Score.pdf",
"11346451":"MUJS 2370 SP2021 Updated Rhythm Changes Drills C - Score.pdf",
"11346453":"MUJS 2370 SP2021 Updated Rhythm Changes Drills Eb - Score.pdf",
"11346454":"MUJS 2370 Updated Rhythm Changes Drills Bass - Score.pdf",
"11346495":"Steinel chromatic ornamentation étude rhythm changes - Aug 23 2020 - 12-19 PM.pdf",
"11346532":"Media Comment-136242b4-7f2c-4d5d-8aa4-091f07e797cf",
"11346581":"2019 Trust Based Relationship Intervention Manual.pdf",
"11346584":"TBRI ppt.pptx",
"11346593":"Trauma informed care.ppt"
}
function NewTab(testing) {
window.open(testing, "_blank");
}
for(test in results) {
var testing = 'https://unt.instructure.com/files/' + test + '/download';
NewTab(testing);
//window.open = 'https://unt.instructure.com/files/' + test + '/download';
}

Timer

0 likes • Feb 5, 2021 • 2 views
JavaScript
class Timer{
start(){
this.startTime = Date.now();
}
stop(){
const dt = Date.now() - this.startTime;
console.log(dt);
return dt;
}
getExecTime(fun, asynchronous){
return asynchronous ? this.getAsyncExecTime(fun) : this.getSyncExecTime(fun);
}
getSyncExecTime(fun){
this.start();
fun();
return this.stop();
}
async getAsyncExecTime(fun){
this.start();
await fun();
return this.stop();
}
static average(fun, numTimes, asynchronous = false){
const times = [];
const promises = [];
let timer = new Timer();
if(asynchronous){
for(let i = 0; i < numTimes; i++){
timer.getAsyncExecTime(fun);
}
}else{
for(let i = 0; i < numTimes; i++){
timer.getSyncExecTime(fun);
}
}
return (times => times.reduce(((sum, acc) => sume + acc), 0) / times.length);
}
static async timeAsync(fun, numTimes){
if(numTimes <= 1) return new Timer().getAsyncExecTime(fun);
const promises = [];
let timer = new Timer(true);
for(let i = 0; i < numTimes; i++){
promises.push(fun().catch((e)=>{}));
}
try{
await Promise.all(promises).catch((e)=>{});
}catch(e){
//do absolutely nothing
}
return timer.stop();
}
constructor(start = false){
if(start) this.start();
}
}

compact object

0 likes • Nov 19, 2022 • 0 views
JavaScript
const compactObject = val => {
const data = Array.isArray(val) ? val.filter(Boolean) : val;
return Object.keys(data).reduce(
(acc, key) => {
const value = data[key];
if (Boolean(value))
acc[key] = typeof value === 'object' ? compactObject(value) : value;
return acc;
},
Array.isArray(val) ? [] : {}
);
};
const obj = {
a: null,
b: false,
c: true,
d: 0,
e: 1,
f: '',
g: 'a',
h: [null, false, '', true, 1, 'a'],
i: { j: 0, k: false, l: 'a' }
};
compactObject(obj);
// { c: true, e: 1, g: 'a', h: [ true, 1, 'a' ], i: { l: 'a' } }

Date Time Testing

0 likes • Nov 18, 2022 • 1 view
JavaScript
var d = new Date();
var d = new Date(milliseconds);
var d = new Date(dateString);
var d = new Date(year, month, day, hours, minutes, seconds, milliseconds);