Skip to content

Instantly share code, notes, and snippets.

View gist:5da1877cef5e5db2b6e7c4169e3796a1
//Have to create a closure and redefine "i" inside so that the console won't throw undefined error
var index = i;
var elem = document.getElementsByClassName('cat');
array[index].count = array[index].count + 1;
var total= array[index].count;
var numb = document.getElementsByTagName("p")[index];
numb.innerHTML = total;
Aldizh / flattenObject.js
Created Sep 15, 2017
Utility function to transform an object to flat key value pairs. It is used to maintain a mapping between an object structure and a general purpose file which could be translated for example.
View flattenObject.js
* Flattens an object given a nested object structure
* @function flattenObject
* @param {Object} obj - The object whose values and keys you want to map over.
* @return {String} - A json string with key-value pairs.
* @example
* const original = {
* address: {
* 'addressLine1': 'Address (Line 1)',
Aldizh / constructObject.js
Created Sep 15, 2017
This is used to convert a flat file into a nested object structure, give the flat file is in proper dot notation.
View constructObject.js
* @function constructObject
* @param {file} file in dot notation ('a.b.c':'Hey', 'd.e.g': 'you')
* @return {Object} - a nested object.
* @example
* const object = constructObject(file)
* Returns the following:
* a: {
* b: {
Aldizh / counter.js
Last active Sep 20, 2019
Basic counter that demonstrates javascript closure concept
View counter.js
// 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 / highest_power
Last active May 1, 2020
Gives you the highest power of x less then provided number n
View highest_power
Problem, find the highest power less than a number n
Iterative solution: O(n)
Logarithmic solution: O(logn)
Bitwise solution: O(logn)
var highestPower = function(n, x) {
var res = 0;
for (var i = n; i >= 1; i--){
var pow = Math.pow(x, i);
Aldizh / reduce_for_objects
Last active May 1, 2020
Utility for reducing array of objects
View reduce_for_objects
Problem: Given an array of objects parse those objects
to form meaningful key value pairs
// Input
const dataSource = [
{id: 48, code: "firstName", description: "John"},
{id: 49, code: "lastName", description: "Smith"},
{id: 49, code: "occupation", description: "Agent"},
Aldizh / git_tips.txt
Last active May 2, 2020
Helpful github tips and commands
View git_tips.txt
# Whenever i start coding i like to start with this command to clean up the repo and find out where we are
$ git fetch --prune origin && git status
# Merging is usually a good strategy as it allows for better tracking and dividing up the work
# Github offers a squash && merge option now on the PRs, but lets consider this:
# You want to test the feature branch locally before merging it and if there are any conflicts you want to resolve and test
$ git checkout master
$ git merge --squash feature_branch
$ git reset origin/master (Optional if we want to unstage all files that differ from origin master)
$ git add --all
Aldizh / Person.js
Last active May 2, 2020
Prototype Inheritance example javascript
View Person.js
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
Aldizh / cellComplete.js
Created Jun 27, 2020
Cell Complete Problem (XOR)
View cellComplete.js
There is a colony of 8 cells arranged in a straight line where each day every cell competes with its adjacent cells(neighbour). Each day, for each cell, if its neighbours are both active or both inactive, the cell becomes inactive the next day,. otherwise itbecomes active the next day.
Assumptions: The two cells on the ends have single adjacent cell, so the other adjacent cell can be assumsed to be always inactive. Even after updating the cell state. consider its pervious state for updating the state of other cells. Update the cell informationof allcells simultaneously.
Write a fuction cellCompete which takes takes one 8 element array of integers cells representing the current state of 8 cells and one integer days representing te number of days to simulate. An integer value of 1 represents an active cell and value of 0 represents an inactive cell.
function changeState(states) {
let temp = []
const len = states.length
const virtualCell = 0
for (var i = 0; i < len; i++) {
You can’t perform that action at this time.