This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
function translateRomanNumeral (romanNumeral) { | |
var DIGIT_VALUES = { | |
I: 1, | |
V: 5, | |
X: 10, | |
L: 50, | |
C: 100, | |
D: 500, | |
M: 1000 |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// 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; |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// 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; |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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]; |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// 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 |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// 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) { |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) { |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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(). |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
function go() { | |
var userId = prompt('Username?', 'Guest'); | |
// Consider adding '/<unique id>' if you have multiple games. | |
var gameRef = new Firebase(GAME_LOCATION); | |
assignPlayerNumberAndPlayGame(userId, gameRef); | |
}; | |
// The maximum number of players. If there are already | |
// NUM_PLAYERS assigned, users won't be able to join the game. | |
var NUM_PLAYERS = 4; |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
function go() { | |
var userId = prompt('Username?', 'Guest'); | |
var userData = { name: userId }; | |
tryCreateUser(userId, userData); | |
} | |
var USERS_LOCATION = 'https://SampleChat.firebaseIO-demo.com/users'; | |
function userCreated(userId, success) { | |
if (!success) { |