Skip to content

Instantly share code, notes, and snippets.

/***
Suppose we have some input data describing a graph of relationships between parents and children over multiple generations. The data is formatted as a list of (parent, child) pairs, where each individual is assigned a unique integer identifier.
For example, in this diagram, 3 is a child of 1 and 2, and 5 is a child of 4:
1 2 4
\ / / \
3 5 8
\ / \ \
6 7 9
function getDecentNumber(N) {
let number = -1,
inc = 5,
five_str = "5",
three_str = "3",
five_num = 5,
three_num = 3;
// numberCast function will turn any string up to 16 digits
@adyngom
adyngom / fn-args-required.js
Last active September 26, 2017 13:56
Javascript ES6 function with required arguments example
const throwIfMissing = (p) => { throw new Error(`Missing parameter: ${p}`); }
function famTree(dad = throwIfMissing("dad"),mom = throwIfMissing("mom"),...siblings) {
console.group("Family");
console.log(`Dad: ${dad}`);
console.log(`Mom: ${mom}`);
if(siblings.length > 0) {
console.log(`Siblings: ${siblings.join()}`);
}
console.groupEnd();
@adyngom
adyngom / sum-nested-array.js
Last active July 21, 2018 17:12
Given a deeply nested array in Javascript find the sum of all the numeric values
// Caculate sum of numbers in nested array
//----------------------------------------//
const arrSet = [[1,2,3],4,5,"h","e",["l","o", null, false, true], 3.75, -1];
const FlattenArray = function(arr) {
return arr.reduce((a,c) => {
return Array.isArray(c) ? a.concat(FlattenArray(c)) : a.concat(c);
}, []);
}
const FlattenArrayByString = function(arr) {
@adyngom
adyngom / extend-array.js
Created September 26, 2017 01:26
Javascript few useful array methods to use without accidentally touching the Array.prototype and create a possible conflict
class List extends Array {
constructor() {
super(...arguments);
}
uniq() {
return [... new Set(this)];
}
empty() {
@adyngom
adyngom / eltonuikit.html
Last active February 13, 2020 09:27
Code for the CSS Grid Quick Overview Tutorial
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Elton ui Kit</title>
<style>
@import 'https://fonts.googleapis.com/css?family=Comfortaa:300,700|Bungee+Shade|Josefin+Sans:400';
html {
background-color: #eee;
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>AutoTab</title>
<style>
body {
min-height: 100vh;
display: flex;
justify-content: center;
@adyngom
adyngom / user-snippet-scaffold.json
Created April 14, 2019 19:55
VS Code User Snippet Scaffold
{
"User snippet": {
"scope": "json",
"prefix": "u-snp",
"body": [
"\"${1:snippet_name}\": {",
"\t\"prefix\": \"${2:snippet_prefix}\",",
"\t\"body\": [",
"\t\t$3",
"\t],",
/**
* Couple of possible solutions to the Sock Merchant challenge on Hacker Rank
* https://www.hackerrank.com/challenges/sock-merchant/problem
**/
// Solution 1
// Video tutorial can be viewed: https://youtu.be/DRp6naqL5uc
function sortAndCount( n, arr ) {
let sorted = arr.sort( (a,b) => a - b);
let pairs = 0;
if(!Array.prototype.uniq) {
Array.prototype.uniq = function() {
return this.reduce( (arr, val) => {
if (!arr.includes(val)) arr.push(val);
return arr;
}, [] )
}
}