Skip to content

Instantly share code, notes, and snippets.

View ernestlv's full-sized avatar
Hello World!

Ernest Leyva ernestlv

Hello World!
View GitHub Profile
@ernestlv
ernestlv / weakmaps.js
Created November 7, 2016 17:59
create private properties in ES6 class using weakmaps
// taken from https://www.sitepoint.com/object-oriented-javascript-deep-dive-es6-classes/
let SimpleDate = (function() {
let _years = new WeakMap();
let _months = new WeakMap();
let _days = new WeakMap();
class SimpleDate {
constructor(year, month, day) {
// Check that (year, month, day) is a valid date
// ...
@ernestlv
ernestlv / fireWhenReady.js
Created February 17, 2018 14:57
Problem is to wait for all ajax calls to finish loading DOM. We count # of DOM elements and wait X secs if DOM changes page still loading. Adjust sec param for different websites
function callWhenReady(callBack, sec) {
var wait4Loop = ( sec || 5 ) * 4;
var loopNum = 0;
var domCount = document.querySelectorAll('*').length; //count DOM elements
var i = setInterval(function(){ // after 1/4 of a sec
var newCount = document.querySelectorAll('*').length; //count DOM elements
if (domCount != newCount){ // new DOM elements are counted, page still loading
domCount = newCount;
loopNum = 0;
}else{ // wait X sec if node count does not change, page likely finished loading.
@ernestlv
ernestlv / smallInt.js
Last active February 18, 2018 21:07
find smallest integer
function smallInt(A) {
let s = 1;
A.sort((a, b) => a - b)
if (A[0] >= 0) {
if (A.indexOf(1) === -1) {
return 1;
}
}
for (i=0; i<A.length; i++){
s = A[i]+1;
@ernestlv
ernestlv / ferrarum2018.js
Created February 19, 2018 02:19
Codility Silver Award for the Ferrum 2018 Challenge
function solution(A) {
var red = A => A.reduce((res, val) => res+val);
for (i=0; i<A.length; i++){
var z = i
for (j=0, z=i; j<=i; j++, z--){
x = red(A.slice(j, A.length-z));
if (x>=0) return A.length-z-j;
}
}
return 0;
@ernestlv
ernestlv / fireWhenReady2.js
Last active February 19, 2018 20:14
Check for specific content and fire when ready.
function callWhenReady(testCB, readyCB) {
console.log('checking for page ready');
var i = setInterval(function(){ // after 1/4 of a sec
var res = testCB();
if (res === true){ // wait X sec if node count does not change, page likely finished loading.
clearInterval(i);
readyCB();
}
}, 250);
}
@ernestlv
ernestlv / oneSwap.js
Created February 21, 2018 00:44
check if you need just one swap to sort a list of number
function isOneSwapSort(A){
for ( let f = A.length - 1; f > 0; f--) {
for (let i=0; i<f; i++) {
if ( A[f] < A[i] ){
let temp = A[i];
A[i] = A[f];
A[f] = temp;
if ( isOneSwapSort(A) > 0 ) return 2; //more than one swap
return 1; //one swap
}
@ernestlv
ernestlv / vacationCalendar.js
Last active February 21, 2018 15:45
calculate # of weeks you can spend in a trip given that you have two months vacations, starting on the 1st day of the first month and finishing on the last day of the second month. you can leave only on mondays and returns only on sundays
function solution(Y, A, B, W) {
// write your code in JavaScript (Node.js 8.9.4)
const MONTH_DAYS = {
"January":31, "February":28, "March":31, "April":30, "May":31, "June":30, "July":31, "August":31, "September":30, "October":31, "November":30, "December":31
};
const MONTHS = {
"January":0, "February":1, "March":2, "April":3, "May":4, "June":5, "July":6, "August":7, "September":8, "October":9, "November":10, "December":11
};
function findFirstVacationMonday(year, month) {
@ernestlv
ernestlv / proxy-json.js
Created March 23, 2018 16:48
Proxy a REST/json request to an external API in Node.js
const http = require('http')
const httpS = require('https')
const Router = require('router')
const port = process.env.PORT || 3000
const router = Router()
router.get('/', (req, res) => {
res.write('REST API is up!');
@ernestlv
ernestlv / array2map.js
Created June 6, 2023 15:24
convert arr to map
/* converts an array of the form [ { key, value } ] to a map of the form { key:value } */
const arr = [{key:"123", value:"abc"}, {key:"456", value:"def"}, {key:"789", value:"ghi"}];
const map = arr.reduce((res, o) => ({ ...res, [o.key]:o }), {});
@ernestlv
ernestlv / diff.js
Last active June 6, 2023 15:49
Calculate the difference between two sets
/* it is in A but not in B */
function diff(arrA, arrB) {
const mapB = arrB.reduce((res, b) => ({ ...res, [b.key]:b }), {});
return arrA.reduce((res, a) => !(a.key in mapB) ? [ ...res, a ] : res, []);
}