From fb0883d2fb666962e143a618dfe673f85908215e Mon Sep 17 00:00:00 2001 From: Tinesha Conway Date: Wed, 11 Nov 2020 14:13:05 -0500 Subject: [PATCH] adding challenges for hw --- add-eventing/add-eventing.js | 14 +++++ babylonian-method/babylonian.js | 8 ++- balanced-parens/balanced-parens.js | 58 ++++++++++++++++++- .../stack-machine-calculator.js | 28 ++++++++- 4 files changed, 103 insertions(+), 5 deletions(-) diff --git a/add-eventing/add-eventing.js b/add-eventing/add-eventing.js index 39ce51c..29e1d2e 100644 --- a/add-eventing/add-eventing.js +++ b/add-eventing/add-eventing.js @@ -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 diff --git a/babylonian-method/babylonian.js b/babylonian-method/babylonian.js index 259fc49..db2c3d4 100644 --- a/babylonian-method/babylonian.js +++ b/babylonian-method/babylonian.js @@ -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 } diff --git a/balanced-parens/balanced-parens.js b/balanced-parens/balanced-parens.js index df43491..f325c94 100644 --- a/balanced-parens/balanced-parens.js +++ b/balanced-parens/balanced-parens.js @@ -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 +*/ \ No newline at end of file diff --git a/stack-machine-calculator/stack-machine-calculator.js b/stack-machine-calculator/stack-machine-calculator.js index 02b4bbe..86dc949 100644 --- a/stack-machine-calculator/stack-machine-calculator.js +++ b/stack-machine-calculator/stack-machine-calculator.js @@ -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 \ No newline at end of file