Skip to content

Instantly share code, notes, and snippets.

View mitrakmt's full-sized avatar

Michael Mitrakos mitrakmt

View GitHub Profile
@mitrakmt
mitrakmt / firebase_remove_pushed.js
Last active September 2, 2015 04:42 — forked from anantn/firebase_remove_pushed.js
Firebase: Removing an item you've pushed. This snippet shows how to remove an object that you just added using push().
function pushSomething(ref) {
// Let's push something. push() returns a reference that you can hold onto!
var justPushed = ref.push({test: "push"});
// We return a reference, but you can also return the name of the newly
// created object with .name().
return justPushed;
}
function removeItem(ref) {
// Now we can get back to that item we just pushed via .child().
@mitrakmt
mitrakmt / firebase_snapshot_parent.js
Last active September 2, 2015 04:42 — forked from anantn/firebase_snapshot_parent.js
Firebase: Get the parent of a snapshot.
function getParent(snapshot) {
// You can get the reference (A Firebase object) from a snapshot
// using .ref().
var ref = snapshot.ref();
// Now simply find the parent and return the name.
return ref.parent().name();
}
var testRef = new Firebase("https://example.firebaseIO-demo.com/foo/bar");
testRef.once("value", function(snapshot) {
@mitrakmt
mitrakmt / stack.js
Created September 19, 2016 03:27
Stack Data Structure in JavaScript
// This Stack is written using the pseudoclassical pattern
// Creates a stack
var Stack = function() {
this.count = 0;
this.storage = {};
}
// Adds a value onto the end of the stack
Stack.prototype.push = function(value) {
@mitrakmt
mitrakmt / queue.js
Created September 19, 2016 03:42
How to implement a Queue in JavaScript
// This Stack is written using the pseudoclassical pattern
// Creates the queue
var Queue = function() {
this.storage = {};
this.count = 0;
this.lowestCount = 0;
}
// Adds a value to the end of the chain
@mitrakmt
mitrakmt / permutations.js
Created September 20, 2016 15:09
All permutations of a set interview challenge.
function getAllPermutations(string) {
var results = [];
if (string.length === 1) {
results.push(string);
return results;
}
for (var i = 0; i < string.length; i++) {
var firstChar = string[i];
@mitrakmt
mitrakmt / evenOccurrence.js
Last active September 26, 2018 16:56
Exploring the programming interview question even occurrence in JavaScript.
// Prompt: Find the first item that occurs an even number of times in an array. Remember to handle multiple even-occurrence items and return the first one. Return null if there are no even-occurrence items.
function evenOccurrence (array) {
// Store counts
var storage = {};
// Store each value within the storage object to keep count
array.forEach(function(value, index) {
storage[value] = storage[value] + 1 || 1;
@mitrakmt
mitrakmt / binarySearchTree.js
Created September 21, 2016 15:11
Implement a Binary Search Tree in JavaScript
// This Binary Search Tree is implemented using the prototypal pattern
var BinarySearchTree = function(value) {
var instance = Object.create(BinarySearchTree.prototype);
instance.value = value;
// a BST where all values are higher than than the current value.
instance.right = undefined;
// a binary search tree (BST) where all values are lower than than the current value.
instance.left = undefined;
@mitrakmt
mitrakmt / translateRomanNumerals.js
Last active September 26, 2016 05:16
Translate Roman Numerals programming interview challenge.
function translateRomanNumeral (romanNumeral) {
var DIGIT_VALUES = {
I: 1,
V: 5,
X: 10,
L: 50,
C: 100,
D: 500,
M: 1000
@mitrakmt
mitrakmt / commonCharacters.js
Created September 26, 2016 14:53
Covering the common characters programming interview question in JavaScript.
const commonCharacters = (string1, string2) => {
const result = {};
string1.split('').forEach(letter => {
if(string2.indexOf(letter) >= 0 && letter !== ' ') {
result[letter] = letter;
}
});
return Object.keys(result).join('');
@mitrakmt
mitrakmt / serveStaticFiles.js
Created September 27, 2016 06:41
How to serve a static file in Node.js, without Express :)
var http = require("http"),
url = require("url"),
path = require("path"),
fs = require("fs"),
mime = require("mime")
port = process.argv[2] || 8888;
http.createServer(function(request, response) {
var uri = url.parse(request.url).pathname