Skip to content

Instantly share code, notes, and snippets.

View Aldizh's full-sized avatar

Aldi Zhupani Aldizh

View GitHub Profile
@Aldizh
Aldizh / counter.js
Last active September 20, 2019 20:17
Basic counter that demonstrates javascript closure concept
// In the function below changeBy is a private function
// whereas increment and decrement are public and hence
// can be invoked outside the Counter's scope
function Counter(initialCounter) {
var privateCounter = initialCounter || 0;
function changeBy(val) {
privateCounter += val;
}
return {
@Aldizh
Aldizh / Person.js
Last active May 2, 2020 22:16
Prototype Inheritance example javascript
/*
Let us start with tehse two definitions:
Class Variables — Declared inside the class definition (but outside any of the instance methods). They are not tied to any particular object of the class, hence shared across all the objects of the class. Modifying a class variable affects all objects instance at the same time.
Instance Variable — Declared inside the constructor method of class (the __init__ method). They are tied to the particular object instance of the class, hence the contents of an instance variable are completely independent from one object instance to the other.
Every function created in javascript inherits from Object
which internally consists of a constructor function and __proto__
__proto__ allows for sharing across instances
*/
// Function to find triplets with zero sum.
/**
* @param {number[]} arr
* @param {number} n
* @returns {boolean}
*/
function findTriplets(arr, n)
{