Skip to content

Instantly share code, notes, and snippets.

View Damimd10's full-sized avatar

Damian Diaz Damimd10

  • Argentina
View GitHub Profile
@Damimd10
Damimd10 / queue.js
Created February 6, 2019 00:28
JavaScript Queue
function createQueue() {
const queue = [];
return {
enqueue(item) {
queue.unshift(item)
},
dequeue() {
return queue.pop()
},
import { createQueue } from './index';
function createPriorityQueue() {
const lowPriorityQueue = createQueue();
const highPriorityQueue = createQueue();
return {
enqueue(item, isHighPriority = false) {
isHighPriority
? highPriorityQueue.enqueue(item)
@Damimd10
Damimd10 / stack.js
Last active February 9, 2019 20:03
function createStack() {
const stack = []
return {
push(item) {
stack.push(item)
},
pop() {
return stack.pop()
},
function createNode(value) {
return {
value,
next: null
}
}
function createLinkedList() {
return {
head: null,
const { createQueue } = require('../queues/index.js')
function createNode(key) {
const children = []
return {
key,
children,
addChild(node) {
children.push(node)
function createNode(key) {
const children = []
return {
key,
children,
addChild(childKey) {
const childNode = createNode(childKey)
children.push(childNode)
return childNode
function createBinaryNode(key) {
return {
key,
left: null,
right: null,
addLeft(leftKey) {
const newLeft = createBinaryNode(leftKey)
this.left = newLeft
return newLeft
},
// global scope
var name = 'Damian';
function getName() {
console.log(name); // 'Damian'
}
// global scope
var name = 'Damian';
function getName(name) { // passing (name) makes it local scope
console.log(a); // log the given argument, not the global value of '1'
}
// local scope again
function localScope() {
var isGlobalScope = false;
var canIVote = 'No';
function areYouOlderThan18() {
if (true) {
var canIVote = 'Yes';
}
console.log(canIVote); // log 'Yes', not the global value of 'No'
}