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 / 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 / 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 / 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 / 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 / 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 / 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 / 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 / timer.html
Last active November 4, 2016 16:30
Timer written in ReactJS
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title>React Timer</title>
<script src="https://unpkg.com/react@15.3.0/dist/react.js"></script>
<script src="https://unpkg.com/react-dom@15.3.0/dist/react-dom.js"></script>
<script src="https://unpkg.com/babel-standalone@6.15.0/babel.min.js"></script>
</head>
<body>
@ernestlv
ernestlv / id.js
Created April 26, 2016 15:10
generates an ID
var id = (Math.random() * (1000000000 - 1) + 1 | 0) + ":" + (new Date().getTime())
@ernestlv
ernestlv / promise4.js
Last active April 27, 2016 22:56
ES7: Executes functions asynchronously using promises and generators.
//invokes segments of an asynchronus function
function asyncF(generator) {
try {
var steps = generator(); // function to exec by segments
return iterate(steps.next());
} catch(e) {
return Promise.reject(e); // rejects iteration
}
function iterate({value, done}) { // arg has value of prev iteration
if (done) return Promise.resolve(value); // resolves all pending promises