Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 14 additions & 0 deletions add-eventing/add-eventing.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,18 @@
const addEventing = function (obj) {
const addEventing = function (obj) {
let events = {}
obj.on = (eventName, fn) => {
if (events[eventName]) {
events[eventName].push(fn)
} else {
events[eventName] = [fn]
}
}
obj.trigger = (eventName, ...args) => {
events[eventName].forEach(fn => fn(...args))
}
return obj
}
}

module.exports = addEventing
8 changes: 7 additions & 1 deletion babylonian-method/babylonian.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,10 @@
const squareRoot = (radicand) => {
function squareRoot(radicand) {
for (let i = radicand; i >= 1; i--) {
if (i * i == radicand) {
radicand = i;
break;
}
}
return radicand
}

Expand Down
58 changes: 56 additions & 2 deletions balanced-parens/balanced-parens.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,59 @@
const parensAreBalanced = (input) => {
return false
const parensAreBalanced = (str) => {
/////// Balanced Parentheses
const parensAreBalanced = (str) => {
let stack = [];
let map = {
'(': ')',
'[': ']',
'{': '}'
}
for (let i = 0; i < str.length; i++) {
if (str[i] === '(' || str[i] === '{' || str[i] === '[' ) {
stack.push(str[i]);
}
else {
let last = stack.pop();
if (str[i] !== map[last]) {
return false
};
}
}
if (stack.length !== 0) {
return false
};
return true;
};
console.log (parensAreBalanced("(){}"));
console.log (parensAreBalanced('{}'));







/*let stack = [];
let map = {
'(': ')',
'[': ']',
'{': '}'
}
for (let i = 0; i < str.length; i++) {
if (str[i] === '(' || str[i] === '{' || str[i] === '[' ) {
stack.push(str[i]);
}
else {
let last = stack.pop();
if (str[i] !== map[last]) {
return false
};
}
}
if (stack.length !== 0) {
return false
};
return true;
}

module.exports = parensAreBalanced
*/
28 changes: 26 additions & 2 deletions stack-machine-calculator/stack-machine-calculator.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,29 @@
const stackMachineCalculator = (instructions) => {
return instructions
let stack = [];
let num = instructions.split("");

for(let i = 0; i < num.length; i++) {
switch(num[i]) {
case "+" :
stack.push(stack.pop() + stack.pop());
break;

case "-" :
stack.push(stack.pop() - stack.pop());
break;

case "*" :
stack.push(stack.pop() * stack.pop());
break;

case "/" :
stack.push(stack.pop() / stack.pop());
break;

default: stack.push(parseInt(num[i]));
}
}
return stack.pop();
}

module.exports = stackMachineCalculator
module.exports = stackMachineCalculator