Javascript Calculator issue resolution while replacing operators at end

There needs issue resolution while handling operators especially while replacing them.
Javascript Calculator

- sign after operator should not be treated as an operator but number and that creates quite a lot of confusion indeed. And to make you feel a little bit better, you’re not the only one who has this problem:

Explanation for those who is curious:
If you look behind just one step (peeking only the last token of the stack) following might occur:
STEP / : After digit so we have operator
STEP - : After operator, so it’s negative number here
STEP . : After minus sign, so we have 0. decimal
… and here’s your bug

There is no easy fix for this and it’s actually quite a problem, because looking two elements back could create another more dangerous patterns.

UPDATE: No, actually bug is a next step and it’s really just a bug in Google’s calculator, silly me :slight_smile: They forget to turn off operators after the dot . - it’s very easy to fix.

This seems working in console.
“5+ -”.replace(/[*/±]-$/, “/”)

if there is any operator and a negative operator then will be replaced with the new input.

New input always being "/"? Not sure this is correct. Try this:

const switchOperatorRe = /[/*\-+]+\s*([/*+])/g;
const keepOperatorRe = /([/*\-+])\s*[/*+]+/g;

const handleChange = (value, strategy) => value
  .replace(strategy === 'keep' ? keepOperatorRe : switchOperatorRe, '$1');

const value = '4-+6/-67++-45+++45/-4-+1+/-3+-4-/-4';

console.log('KEEP: ', handleChange(value, 'keep'));
console.log('SWITCH: ', handleChange(value, 'switch'));

It won’t solve case -- => +, for that your would need another handler, but I’m sure you will come with solution! Good luck!