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 calculate(obj) { | |
| switch (obj.type) { | |
| case "number": | |
| return parseInt(obj.value); | |
| case "^": | |
| return calculate(obj.left) ** calculate(obj.right); | |
| case "+": | |
| return calculate(obj.left) + calculate(obj.right); | |
| case "-": | |
| return calculate(obj.left) - calculate(obj.right); |
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 parse(fullString) { | |
| let strArray = fullString.split(" "); | |
| let pos = 0; | |
| function checkPos() { | |
| return strArray[pos]; | |
| } | |
| function isNumber(e) { | |
| return e !== undefined && e.match(/^-?([1-9][0-9]*|([0]))$/) !== null; |
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 parse(fullString) { | |
| let strArray = fullString.split(" "); | |
| let pos = 0; | |
| function checkPos() { | |
| return strArray[pos]; | |
| } | |
| function isNumber(e) { | |
| return e !== undefined && e.match(/^-?([1-9][0-9]*|([0]))$/) !== null; |
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 calculate(obj) { | |
| switch (obj.type) { | |
| case "number": | |
| return parseInt(obj.value); | |
| case "+": | |
| return calculate(obj.left) + calculate(obj.right); | |
| case "-": | |
| return calculate(obj.left) - calculate(obj.right); | |
| case "*": | |
| return calculate(obj.left) * calculate(obj.right); |
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 splitString(str) { | |
| let seperatedString = str.split(" "); | |
| if ( | |
| !seperatedString.every( | |
| (e) => /^-?([1-9][0-9]*|([0]))$|^[\+\*\/\-]$/.test(e) | |
| ) | |
| ) { | |
| throw SyntaxError("Invalid Format"); | |
| } |
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
| const calcString = (str) => { | |
| let seperatedStr = str.split(" "); | |
| if ( | |
| !seperatedStr.every((e) => /^-?([1-9][0-9]*|([0]))$|^[\+\*\/\-]$/.test(e)) // Wanted to check input validity, can be removed | |
| ) { | |
| throw SyntaxError("Invalid Format"); | |
| } | |
| let definedOps = [ | |
| { "*": (x, y) => x * y, "/": (x, y) => x / y }, |
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
| let testStrings = [ | |
| { str: "1 + 1", ans: 2 }, | |
| { str: "2 * 8", ans: 16 }, | |
| { str: "6 - 0", ans: 6 }, | |
| { str: "4 / 2", ans: 2 }, | |
| { str: "5 + 7 * 2 - 4", ans: 15 }, | |
| { str: "15 - 10 * -2", ans: 35 }, | |
| { str: "1050 + 7 - 56 * 2", ans: 945 }, | |
| { str: "50 / 2 / 5 * 3 - -10", ans: 25 }, | |
| { str: "0 + 1 - -2 * -3", ans: -5 }, |
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
| var a = 1; | |
| function b() { | |
| var a; | |
| a = function () {} | |
| a = 10; | |
| return; | |
| } | |
| b(); | |
| alert(a); |
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
| var a = 1; | |
| function b() { | |
| a = 10; | |
| return; | |
| function a() {} | |
| } | |
| b(); | |
| alert(a); |
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
| var foo = 1; | |
| function bar() { | |
| var foo; | |
| if (!foo) { | |
| foo = 10; | |
| } | |
| alert(foo); | |
| } | |
| bar(); |
NewerOlder