Skip to content

Instantly share code, notes, and snippets.

View kermit-klein's full-sized avatar
🏠
Working from home

Ali Erbay kermit-klein

🏠
Working from home
View GitHub Profile
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);
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;
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;
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);
function splitString(str) {
let seperatedString = str.split(" ");
if (
!seperatedString.every(
(e) => /^-?([1-9][0-9]*|([0]))$|^[\+\*\/\-]$/.test(e)
)
) {
throw SyntaxError("Invalid Format");
}
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 },
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 },
@kermit-klein
kermit-klein / example.js
Created November 22, 2020 18:06
hoist6
var a = 1;
function b() {
var a;
a = function () {}
a = 10;
return;
}
b();
alert(a);
@kermit-klein
kermit-klein / example.js
Created November 22, 2020 16:18
hoist5
var a = 1;
function b() {
a = 10;
return;
function a() {}
}
b();
alert(a);
@kermit-klein
kermit-klein / example.js
Created November 22, 2020 15:32
hoist4
var foo = 1;
function bar() {
var foo;
if (!foo) {
foo = 10;
}
alert(foo);
}
bar();