Disclaimer: Not a Code Reviewer
Your code looks pretty good! Just briefly commenting:
My guess is that maybe you might want to design some low-complexity algorithms to do the parsing (if not using an already developed parser – which would have been my first choice – browsing through GitHub), rather than using operational-intensive string manipulations with fledgling regular expressionsfledgling regular expressions.
Here is just an example using stack:
const parser = function(s) {
if (s.length === 0) {
return 0;
}
let stack = [];
let operation = "+";
for (let index = 0, num = 0; index <= s.length; ++index) {
if (s[index] === ' ') {
continue;
}
if (s[index] >= '0' && s[index] <= '9') {
num *= 10;
num += parseInt(s[index]);
continue;
}
if (operation === '+') {
stack.push(num);
} else if (operation === '-') {
stack.push(-num);
} else if (operation === '*') {
stack.push(stack.pop() * num);
} else if (operation === '/') {
stack.push(Math.trunc(stack.pop() / num));
}
operation = s[index];
num = 0;
}
return stack.reduce((a, b) => a + b, 0);
};
console.log(parser(" 1 + 2 + 3 * 4 * 5 * 6 + 3 "));
Happy Coding!! ( ˆ_ˆ )
Reference
Disclaimer: Not a Code Reviewer
Your code looks pretty good! Just briefly commenting:
My guess is that maybe you might want to design some low-complexity algorithms to do the parsing (if not using an already developed parser – which would have been my first choice – browsing through GitHub), rather than using operational-intensive string manipulations with fledgling regular expressions.
Here is just an example using stack:
const parser = function(s) {
if (s.length === 0) {
return 0;
}
let stack = [];
let operation = "+";
for (let index = 0, num = 0; index <= s.length; ++index) {
if (s[index] === ' ') {
continue;
}
if (s[index] >= '0' && s[index] <= '9') {
num *= 10;
num += parseInt(s[index]);
continue;
}
if (operation === '+') {
stack.push(num);
} else if (operation === '-') {
stack.push(-num);
} else if (operation === '*') {
stack.push(stack.pop() * num);
} else if (operation === '/') {
stack.push(Math.trunc(stack.pop() / num));
}
operation = s[index];
num = 0;
}
return stack.reduce((a, b) => a + b, 0);
};
console.log(parser(" 1 + 2 + 3 * 4 * 5 * 6 + 3 "));
Happy Coding!! ( ˆ_ˆ )
Reference
Disclaimer: Not a Code Reviewer
Your code looks pretty good! Just briefly commenting:
My guess is that maybe you might want to design some low-complexity algorithms to do the parsing (if not using an already developed parser – which would have been my first choice – browsing through GitHub), rather than using operational-intensive string manipulations with fledgling regular expressions.
Here is just an example using stack:
const parser = function(s) {
if (s.length === 0) {
return 0;
}
let stack = [];
let operation = "+";
for (let index = 0, num = 0; index <= s.length; ++index) {
if (s[index] === ' ') {
continue;
}
if (s[index] >= '0' && s[index] <= '9') {
num *= 10;
num += parseInt(s[index]);
continue;
}
if (operation === '+') {
stack.push(num);
} else if (operation === '-') {
stack.push(-num);
} else if (operation === '*') {
stack.push(stack.pop() * num);
} else if (operation === '/') {
stack.push(Math.trunc(stack.pop() / num));
}
operation = s[index];
num = 0;
}
return stack.reduce((a, b) => a + b, 0);
};
console.log(parser(" 1 + 2 + 3 * 4 * 5 * 6 + 3 "));
Happy Coding!! ( ˆ_ˆ )
Reference
Disclaimer: Not a Code Reviewer
Your code looks pretty good! Just briefly commenting:
My guess is that maybe you might want to design some low-complexity algorithms to do the parsing (if not using an already developed parser – which would have been my first choice – browsing through GitHub), rather than using operational-intensive string manipulations with fledgling regular expressions.
Here is just an example using stack:
const parser = function(s) {
if (s.length === 0) {
return 0;
}
let stack = [];
let operation = "+";
for (let index = 0, num = 0; index <= s.length; ++index) {
if (s[index] === ' ') {
continue;
}
if (s[index] >= '0' && s[index] <= '9') {
num *= 10;
num += parseInt(s[index]);
continue;
}
if (operation === '+') {
stack.push(num);
} else if (operation === '-') {
stack.push(-num);
} else if (operation === '*') {
stack.push(stack.pop() * num);
} else if (operation === '/') {
stack.push(Math.trunc(stack.pop() / num));
}
operation = s[index];
num = 0;
}
return stack.reduce((a, b) => a + b, 0);
};
console.log(parser(" 1 + 2 + 3 * 4 * 5 * 6 + 3 "));